From: Christophe CURIS <[email protected]>
When using the formula [sizeof(array) / sizeof( x )] to get the number
of element in a static array, it is better to use array[0] for 'x'
instead of the base type of array:
- in case the base type would change someday;
- if the compiler were deciding to insert padding somewhere
---
WINGs/wfontpanel.c | 2 +-
WINGs/widgets.c | 6 +++---
WPrefs.app/Appearance.c | 2 +-
WPrefs.app/MouseSettings.c | 4 ++--
WPrefs.app/Workspace.c | 2 +-
src/defaults.c | 8 ++++----
src/startup.c | 6 +++---
src/wmspec.c | 2 +-
src/xutil.c | 2 +-
util/fontconv.c | 2 +-
10 files changed, 18 insertions(+), 18 deletions(-)
diff --git a/WINGs/wfontpanel.c b/WINGs/wfontpanel.c
index 0a99c3e..c6df7b3 100644
--- a/WINGs/wfontpanel.c
+++ b/WINGs/wfontpanel.c
@@ -428,7 +428,7 @@ static void addSizeToTypeface(Typeface * face, int size)
if (size == 0) {
int j;
- for (j = 0; j < sizeof(scalableFontSizes) / sizeof(int); j++) {
+ for (j = 0; j < sizeof(scalableFontSizes) /
sizeof(scalableFontSizes[0]); j++) {
size = scalableFontSizes[j];
if (!WMCountInArray(face->sizes, (void *)(uintptr_t)
size)) {
diff --git a/WINGs/widgets.c b/WINGs/widgets.c
index 39cc3ef..5aa3e80 100644
--- a/WINGs/widgets.c
+++ b/WINGs/widgets.c
@@ -552,7 +552,7 @@ WMScreen *WMCreateScreenWithRContext(Display * display, int
screen, RContext * c
"_NET_WM_ICON_NAME",
"_NET_WM_ICON",
};
- Atom atoms[sizeof(atomNames) / sizeof(char *)];
+ Atom atoms[sizeof(atomNames) / sizeof(atomNames[0])];
int i;
if (!initialized) {
@@ -803,9 +803,9 @@ WMScreen *WMCreateScreenWithRContext(Display * display, int
screen, RContext * c
}
#ifdef HAVE_XINTERNATOMS
- XInternAtoms(display, atomNames, sizeof(atomNames) / sizeof(char *),
False, atoms);
+ XInternAtoms(display, atomNames, sizeof(atomNames) /
sizeof(atomNames[0]), False, atoms);
#else
- for (i = 0; i < sizeof(atomNames) / sizeof(char *); i++) {
+ for (i = 0; i < sizeof(atomNames) / sizeof(atomNames[0]); i++) {
atoms[i] = XInternAtom(display, atomNames[i], False);
}
#endif
diff --git a/WPrefs.app/Appearance.c b/WPrefs.app/Appearance.c
index 3dfe891..dc7c1db 100644
--- a/WPrefs.app/Appearance.c
+++ b/WPrefs.app/Appearance.c
@@ -1102,7 +1102,7 @@ static void previewClick(XEvent * event, void *clientData)
switch (panel->oldTabItem) {
case 0:
- for (i = 0; i < sizeof(previewPositions) / sizeof(WMRect); i++)
{
+ for (i = 0; i < sizeof(previewPositions) /
sizeof(previewPositions[0]); i++) {
if (event->xbutton.x >= previewPositions[i].pos.x
&& event->xbutton.y >= previewPositions[i].pos.y
&& event->xbutton.x < previewPositions[i].pos.x
diff --git a/WPrefs.app/MouseSettings.c b/WPrefs.app/MouseSettings.c
index ca709e5..5291c0d 100644
--- a/WPrefs.app/MouseSettings.c
+++ b/WPrefs.app/MouseSettings.c
@@ -608,13 +608,13 @@ static void createPanel(Panel * p)
WMResizeWidget(panel->wheelP, 135, 20);
WMMoveWidget(panel->wheelP, 95, 129);
- for (i = 0; i < sizeof(buttonActions) / sizeof(char *); i++) {
+ for (i = 0; i < sizeof(buttonActions) / sizeof(buttonActions[0]); i++) {
WMAddPopUpButtonItem(panel->button1P, buttonActions[i]);
WMAddPopUpButtonItem(panel->button2P, buttonActions[i]);
WMAddPopUpButtonItem(panel->button3P, buttonActions[i]);
}
- for (i = 0; i < sizeof(wheelActions) / sizeof(char *); i++) {
+ for (i = 0; i < sizeof(wheelActions) / sizeof(wheelActions[0]); i++) {
WMAddPopUpButtonItem(panel->wheelP, wheelActions[i]);
}
diff --git a/WPrefs.app/Workspace.c b/WPrefs.app/Workspace.c
index ce6ff54..d84a393 100644
--- a/WPrefs.app/Workspace.c
+++ b/WPrefs.app/Workspace.c
@@ -81,7 +81,7 @@ static void showData(_Panel * panel)
str = "center";
idx = 1; /* center */
- for (i = 0; i < sizeof(WSNamePositions) / sizeof(char *); i++) {
+ for (i = 0; i < sizeof(WSNamePositions) / sizeof(WSNamePositions[0]);
i++) {
if (strcasecmp(WSNamePositions[i], str) == 0) {
idx = i;
break;
diff --git a/src/defaults.c b/src/defaults.c
index a2d6e21..2a75b73 100644
--- a/src/defaults.c
+++ b/src/defaults.c
@@ -760,7 +760,7 @@ static void initDefaults()
WMPLSetCaseSensitive(False);
- for (i = 0; i < sizeof(optionList) / sizeof(WDefaultEntry); i++) {
+ for (i = 0; i < sizeof(optionList) / sizeof(optionList[0]); i++) {
entry = &optionList[i];
entry->plkey = WMCreatePLString(entry->key);
@@ -770,7 +770,7 @@ static void initDefaults()
entry->plvalue = NULL;
}
- for (i = 0; i < sizeof(staticOptionList) / sizeof(WDefaultEntry); i++) {
+ for (i = 0; i < sizeof(staticOptionList) / sizeof(staticOptionList[0]);
i++) {
entry = &staticOptionList[i];
entry->plkey = WMCreatePLString(entry->key);
@@ -925,7 +925,7 @@ void wReadStaticDefaults(WMPropList * dict)
unsigned int i;
void *tdata;
- for (i = 0; i < sizeof(staticOptionList) / sizeof(WDefaultEntry); i++) {
+ for (i = 0; i < sizeof(staticOptionList) / sizeof(staticOptionList[0]);
i++) {
entry = &staticOptionList[i];
if (dict)
@@ -1079,7 +1079,7 @@ void wReadDefaults(WScreen * scr, WMPropList * new_dict)
needs_refresh = 0;
- for (i = 0; i < sizeof(optionList) / sizeof(WDefaultEntry); i++) {
+ for (i = 0; i < sizeof(optionList) / sizeof(optionList[0]); i++) {
entry = &optionList[i];
if (new_dict)
diff --git a/src/startup.c b/src/startup.c
index d309d98..1532626 100644
--- a/src/startup.c
+++ b/src/startup.c
@@ -482,7 +482,7 @@ void StartUp(Bool defaultScreenOnly)
#ifdef HAVE_XRANDR
int dummy;
#endif
- Atom atom[sizeof(atomNames) / sizeof(char *)];
+ Atom atom[sizeof(atomNames) / sizeof(atomNames[0])];
/*
* Ignore CapsLock in modifiers
@@ -505,12 +505,12 @@ void StartUp(Bool defaultScreenOnly)
/* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
#ifdef HAVE_XINTERNATOMS
- XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(char *), False,
atom);
+ XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(atomNames[0]),
False, atom);
#else
{
int i;
- for (i = 0; i < sizeof(atomNames) / sizeof(char *); i++)
+ for (i = 0; i < sizeof(atomNames) / sizeof(atomNames[0]); i++)
atom[i] = XInternAtom(dpy, atomNames[i], False);
}
#endif
diff --git a/src/wmspec.c b/src/wmspec.c
index 255982e..149eab1 100644
--- a/src/wmspec.c
+++ b/src/wmspec.c
@@ -202,7 +202,7 @@ static atomitem_t atomNames[] = {
{"UTF8_STRING", &utf8_string},
};
-#define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
+#define atomNr (sizeof(atomNames)/sizeof(atomNames[0]))
#define _NET_WM_STATE_REMOVE 0
#define _NET_WM_STATE_ADD 1
diff --git a/src/xutil.c b/src/xutil.c
index fc3ebd1..3d1e83e 100644
--- a/src/xutil.c
+++ b/src/xutil.c
@@ -168,7 +168,7 @@ void FormatXError(Display * dpy, XErrorEvent * error, char
*buffer, int size)
if (i > size - 100)
return;
buffer += i;
- if (error->request_code >= sizeof(requestCodes) / sizeof(char *)) {
+ if (error->request_code >= sizeof(requestCodes) /
sizeof(requestCodes[0])) {
sprintf(buffer, "\n Request code: %i\n",
error->request_code);
} else {
sprintf(buffer, "\n Request code: %i %s\n",
error->request_code,
diff --git a/util/fontconv.c b/util/fontconv.c
index 4c420cc..19204db 100644
--- a/util/fontconv.c
+++ b/util/fontconv.c
@@ -105,7 +105,7 @@ static char *mapWeightToName(str * weight)
if (weight->len == 0)
return "";
- for (i = 0; i < sizeof(normalNames) / sizeof(char *); i++) {
+ for (i = 0; i < sizeof(normalNames) / sizeof(normalNames[0]); i++) {
if (strlen(normalNames[i]) == weight->len &&
strncmp(normalNames[i], weight->str, weight->len)) {
return "";
}
--
1.7.10.4
--
To unsubscribe, send mail to [email protected].