printf("\nThe Result is : %s %s %s %s %s %s %s %s\n",var[0],var[1],var[2],var[3],var[4],var[5],var[6],var[7]);

while executing this i get correct result upto var[4]....
and for var[5] and var[6] im geting some garbage valuess, then for var[7] im getg my result...
whats wrong in my code.....

Dani AI

Generated

The pattern — correct output for some indices, garbage for others, then a correct value again — points to undefined behaviour in how var is declared or filled, not to printf itself. 's loop suggestion is a good simplification for printing, and / were right to zero in on the declaration; 's request for the declaration is exactly the missing detail needed to diagnose this. Intermittent garbage usually means a bad pointer, a missing NUL terminator, or a memory overwrite that occasionally leaves one index looking valid.

Common safe declarations/uses to compare against the OP's unknown var:

/* fixed-size 2D array: each element has room for the NUL */
char var[8][32] = { "one", "two", /* ... */ };

/* array of pointers to string literals (read-only) */
const char *var[8] = { "one", "two", NULL, /* ... */ };

/* pointers with dynamic allocation (remember +1 for NUL) */
char *var[8] = {0};
/* var[5] = malloc(len + 1); snprintf(var[5], len+1, "%s", source); */

Practical troubleshooting checklist (in order): enable compiler warnings (-Wall -Wextra -Wformat), build with AddressSanitizer (-fsanitize=address) or run under Valgrind to catch overruns/use-after-free, and inspect pointers/lengths at runtime (print pointer values with %p and lengths with strlen) to see which elements are NULL, dangling, or non-terminated. Also verify every element you pass to %s is a valid char* pointing to a NUL-terminated buffer — a mismatched type in a variadic call invokes undefined behaviour (see printf argument rules).

Authoritative references: see the C printf specification and argument requirements at and the general discussion of undefined behaviour in C at . For memory-debugging tools, see Valgrind.

Recommended Answers

All 6 Replies

Hi,
Think there's a problem with var[] array.mention details

wouldn't it have been a lot simpler to have coded it with a loop

int i;
printf(\nThe Result is : ");
for(i = 0; i < 7; i++)
   printf("%s ", var[i]);

>>whats wrong in my code.....
how did you declare and initialize array var ? Most likely the problem is uninitialized data.

ummmm....
how can you giv print to a single variable var with %s specifier???

your problem shouldn't be in the code but the definition of var[]!

ummmm....
how can you giv print to a single variable var with %s specifier???

Are you referring to the code I posted? I'm assuming var is an array of character arrays. It works because I used a loop that starts the variable i at 0 and increments it by one on each loop iteration. If you don't know about loops yet then you should read up on them.

ummmm....
how can you giv print to a single variable var with %s specifier???

your problem shouldn't be in the code but the definition of var[]!

We don't know how it is declared, do we? It might be a 2D array or an array of Pointers or may be even vectors, the op should give more details if he/she wants a good answer

commented: Yeah, that and many other possible horrors +28

Are you referring to the code I posted? I'm assuming var is an array of character arrays. It works because I used a loop that starts the variable i at 0 and increments it by one on each loop iteration. If you don't know about loops yet then you should read up on them.

i meant to reply to op's post!
he should hav posted the definition of the the array for better understanding of the problem!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.