debugger
debugger

Reputation: 31

strings and pointers manipulation

I am trying to print all the 4 strings in the given program. But I am unable to print the strings. With the code below I get some weird output.

int main()
{
    char szStr[] = "India\0Japan\0America\0Australia";
    char *p = szStr;

    while(p)
    {
        printf("%c", p);
        p++;
    }

    return 0;
} 

Upvotes: 1

Views: 122

Answers (5)

pkacprzak
pkacprzak

Reputation: 5629

Remember that *p gets you the value pointed by p.

This will work:

int main()
{
    char szStr[] = "India\0Japan\0America\0Australia\0";
    char *p = szStr;
    int numOfStrings = 4;
    for(int i = 0; i < numOfStrings; i++)
    {
        while(*p)
        {
            printf("%c", *p);
            p++;
        }
        printf(" "); //print a space character to separate your strings
        p++; //advance pointer to skip '\0' between strings
    }    
    return 0;
}

Upvotes: 0

user529758
user529758

Reputation:

while(p)

is the same as

while(p != NULL)

which is of course always true - the string literal points to a valid memory location. I feel you are confusing this with

while(*p)

or with the equivalent

while(*p != 0)

because that would run until it finds the 0-terminator in the string. That's not good enough either, though. You don't know which string you just printed. You have to keep track of the number of strings manually. Also, why print it character-by-character, invoking the quite expensive printf() function for every byte you display? It's just wasteful. How about something like

char szStr[] = "India\0Japan\0America\0Australia";

char *p = szStr;

for (int i = 0; i < 4; i++) {
    puts(p);
    p += strlen(p) + 1;
}

Still, I don't see why this is simpler or otherwise better than simply storing an array of strings. Like this:

const char *strings[] = {
    "India",
    "Japan",
    "America",
    "Australia"
};

for (int i = 0; i < sizeof(strings) / sizeof(strings[0]); i++)
    puts(strings[i]);

Upvotes: 4

brianmearns
brianmearns

Reputation: 9967

This is tested and works:

#include <stdio.h>

int main()
{
    char szStr[] = "India\0Japan\0America\0Australia\0";
    char *p = szStr;

    while(*p)
    {
        p += printf("%s", p);
        p++;
        fputc('\n', stdout);
    }

    return 0;
}

Note the extra null-terminator on the string. This is actually important, despite the fact that the string literal has it's own null-term. The double null-term is necessary to terminate the while loop correctly.

Explanation:

printf returns the number of characters written, i.e., the length of each string, not including the null terminator. So when you add this to p, you're left with p pointing at the null-terminator. Increment p again and you're at the start of the next string. If that string is length-0, then p is once again pointing to a null-terminator, so the while loop will bail. That's why you need the extra null-terminator on the end of the string.

Upvotes: 2

Govind Balaji
Govind Balaji

Reputation: 639

Change the line while(p) to while(*p).

In c, anything other than 0 will be considered true and 0 will be considered false.

So, p is not 0 and it makes the while loop repeat ever and ever again.

But *p will become 0 at the end of the string.

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

%c is format specifier for printing a single character. Use %s instead. Still printf will only print the string up to the first \0 character. You will have to perform some kind of split before printing if you want the four strings to be output. Maybe use sscanf with "%s%s%s%s".

Upvotes: 0

Related Questions