-
printf format problem
i was following a tutorial at:Learn C The Hard Way A Clear & Direct Introduction To Modern C Programming
on exercise 15 i am trying the extra credit for rewriteing the program using functions.
here is my code:
Code:
#include <stdio.h>
void indexarrayPrint();
int pointerPrint1( int *x, char **y);
int pointerPrint2( int *x, char **y);
int pointerPrint3( int *x, char **y);
int pointerPrint4( int *x, char **y);
int main(int argc, char *argv)
{
int ages[] = {23, 43, 12, 89, 2};
char *names[] = {
"Alan", "Frank",
"Mary", "John", "Lisa"
};
int *cur_age = ages;
char **cur_name = names;
indexarrayPrint(names, ages);
pointerPrint1( cur_age, cur_name);
pointerPrint2( cur_age, cur_name);
pointerPrint3( cur_age, cur_name);
pointerPrint4( cur_age, cur_name);
return 0;
}
void indexarrayPrint()
{
int ages[] = {23, 43, 12, 89, 2};
char *names[] = {
"Alan", "Frank",
"Mary", "John", "Lisa"
};
printf("indexarrayPrint: \n");
int count = sizeof(ages) / sizeof(int);
int i = 0;
for(i = 0; i < count; i++) {
printf("%s has %d years alive.\n", names[i], ages[i]);
}
printf("---\n");
}
int pointerPrint1( int *x, char **y)
{
printf("pointerPrint1: \n");
int ages[] = {23, 43, 12, 89, 2};
int count = sizeof(ages) / sizeof(int);
int i = 0;
for(i = 0; i < count; i++) {
printf("%s is %d years old.\n", *(y+i), *(x+i));
}
printf("---\n");
}
int pointerPrint2( int *x, char **y)
{
printf("pointerPrint2: \n");
int ages[] = {23, 43, 12, 89, 2};
int count = sizeof(ages) / sizeof(int);
int i = 0;
for (i = 0; i < count; i++) {
printf("%s is %d years old again.\n", y[i], x[i]);
}
printf("---\n");
}
/* This Prints Garbadge... WIP */
int pointerPrint3( int *x, char **y)
{
printf("pointerPrint3: \n");
int ages[] = {23, 43, 12, 89, 2};
char *names[] = {
"Alan", "Frank",
"Mary", "John", "Lisa"
};
int count = sizeof(ages) / sizeof(int);
int i = 0;
for (y = names, x = ages; (x - ages) < count; x++, y++) {
printf("%s lived %d years so far.\n", y, x);
}
printf("---\n");
}
int pointerPrint4( int *x, char **y)
{
printf("pointerPrint4: \n");
int ages[] = {23, 43, 12, 89, 2};
int count = sizeof(ages) / sizeof(int);
int i = 0;
for (i = 0; i < count; i++) {
printf("%p lived %p years ago...\n", &y, &x);
}
printf("---\n");
}
it works but i got 2 warnings:
Code:
ex15_function.c: In function ‘pointerPrint3’:ex15_function.c:102:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
ex15_function.c:102:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat]
the function pointerPrint3 gives garbade output in the %s and %d areas, how do i format this to get rid of the warnings and have it print correctly?
-
x and y are pointers to the things you want to give printf, i.e. a pointer to an int and a pointer to a string (pointer to a char pointer). printf just wants a plain int and a plain string (char pointer). You need to dereference them to get the thing they point at. Put a * in front of each of them:
Code:
printf("%s lived %d...", *y, *x);
-
Thank You, its all good now, i will keep that in my memory for the future.
I am glad i asked, i was kinda embarrassed i was getting this kind of error.