Warnung bei printf und Addresseausgabe
-
Hallo,
Beim kompilieren von dem Code unten, bekomme ich folgende warning:warning: void format, different type arg (arg 3)
Dabei habe ich folgenden Code mit dem gcc und folgenden Flags compiliert:
-g -Wall -std=c99 -pedanticFrage:
Warum bekomme ich hier eine Warnung und wie mache ich es richtig?
TIA#include <stdlib.h> #include <stdio.h> #include <string.h> #define TEXT "foobar" extern char* strcpy(char*, const char*); int main(int argc, char** argv) { int iStr_size = strlen(TEXT) + 1; char* pStr = NULL; if( (pStr = calloc(iStr_size, sizeof(*pStr))) == NULL) return EXIT_FAILURE; strcpy(pStr, TEXT); // void pointer void* pVoid; printf("init:\n\tiStr_size = %d\n\tpStr = \"%s\"\n", iStr_size, pStr); printf("\n"); // pVoid -> int printf("init pVoid to int:\n"); pVoid = (int*) &iStr_size; printf("\tpVoid = %p\tiStr_size = %p\n", pVoid, &iStr_size); // WARNING?! printf("\tpVoid = %d\tiStr_size = %d\n", *((int*) pVoid), iStr_size); printf("\n"); // free printf("free()\n"); if(pStr != NULL) free(pStr); printf("READY.\n"); return EXIT_SUCCESS; }
-
Es ist nicht falsch, es liegt an der '-pedantic' Option (die meiner Meinung nach viel zu viel warnt). Die Warnung sagt
mist.c:24: warning: format '%p' expects type 'void *', but argument 3 has type 'int *'
Das dritte Argument ist nämlich "&iStr_size" und das ist ein int* und kein void*. Laut man: printf(3) erwartet "%p" einen void-Zeiger, darum die Warnung (die erst durch -pedantic angezeigt wird).
-
Ja, so hab ich das auch irgendwie verstanden, aber wie macht man es denn "richtig"? Sollte ich hier etwa die Adresse der Intvariable auf einen dereferenzierten void* casten??
Irgendeinen Sinn muss diese Warnung doch haben, oder nicht?
-
es ist schon richtig, wie du es getan hast. Einen void* Cast, nur um den ISO C glückglich zu machen, finde ich nicht sinnvoll. Mein gcc man page meint zu -pedantic folgendes:
-pedantic
Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs
that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.Valid ISO C and ISO C++ programs should compile properly with or without this option (though a rare few will require -ansi or a -std option
specifying the required version of ISO C). However, without this option, certain GNU extensions and traditional C and C++ features are
supported as well. With this option, they are rejected.-pedantic does not cause warning messages for use of the alternate keywords whose names begin and end with __. Pedantic warnings are also
disabled in the expression that follows "__extension__". However, only system header files should use these escape routes; application
programs should avoid them.**
Some users try to use -pedantic to check programs for strict ISO C conformance. They soon find that it does not do quite what they want:
it finds some non-ISO practices, but not all---only those for which ISO C requires a diagnostic, and some others for which diagnostics have
been added.
**A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and
would be quite different from -pedantic. We don't have plans to support such a feature in the near future.Where the standard specified with -std represents a GNU extended dialect of C, such as gnu89 or gnu99, there is a corresponding base stan-
dard, the version of ISO C on which the GNU extended dialect is based. Warnings from -pedantic are given where they are required by the
base standard. (It would not make sense for such warnings to be given only for features not in the specified GNU C dialect, since by defi-
nition the GNU dialects of C include all features the compiler supports with the given option, and there would be nothing to warn about.)Wenn du strict ISO C conformance brauchst/benötigst/willst, dann musst du casten. Ich finde es an der Stelle sinnlos, selbst wenn es nicht strikt ISO C konform ist.
-
Ok, ich gluab ich hab's begriffen -pedantic heisst wirklich "pedantisch"
Danke!