G
gaga
I can't seem to get this to work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *names[3];
char **np;
names[0] = "jack";
names[1] = "jill";
names[2] = "zack";
while (**np != '\0') {
printf("%s\n",*np);
np++;
}
return 0;
}
after printing the 3 names, it prints garbage then sometimes seg faults.
I know i can do it easily using a for loop, but that's not what I am looking
for. I was under the impression names is actually:
[] --> "jack\0"
[] --> "jill\0"
[] --> "zack\0"
[] --> \0
If, that is so, shouldn't I be able to perform the above loop?
This works:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *names[3];
char **np;
names[0] = "jack";
names[1] = "jill";
names[2] = "zack";
while (**np != '\0')
printf("\%s\n",*(*np)++);
np++;
while (**np != '\0')
printf("\%s\n",*(*np)++);
np++;
while (**np != '\0')
printf("\%s\n",*(*np)++);
return 0;
{
But, I need 3 loops for what I'd like to do in one.
I do not want to use the fact that I know there are n
strings in the array. I want to use the fact that each string is
null terminated then there is a final null terminator.
any suggestions would be appreciated.
- gaga
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *names[3];
char **np;
names[0] = "jack";
names[1] = "jill";
names[2] = "zack";
while (**np != '\0') {
printf("%s\n",*np);
np++;
}
return 0;
}
after printing the 3 names, it prints garbage then sometimes seg faults.
I know i can do it easily using a for loop, but that's not what I am looking
for. I was under the impression names is actually:
[] --> "jack\0"
[] --> "jill\0"
[] --> "zack\0"
[] --> \0
If, that is so, shouldn't I be able to perform the above loop?
This works:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *names[3];
char **np;
names[0] = "jack";
names[1] = "jill";
names[2] = "zack";
while (**np != '\0')
printf("\%s\n",*(*np)++);
np++;
while (**np != '\0')
printf("\%s\n",*(*np)++);
np++;
while (**np != '\0')
printf("\%s\n",*(*np)++);
return 0;
{
But, I need 3 loops for what I'd like to do in one.
I do not want to use the fact that I know there are n
strings in the array. I want to use the fact that each string is
null terminated then there is a final null terminator.
any suggestions would be appreciated.
- gaga