2

I wrote a little piece of code to test whether the array of pointer is working as I expected. Then I got this wired results -- After the third pointer assignment, the array of pointer all point to the last string. Anyone can explain what happened? Thank you.

#include <string.h>
#include <stdio.h>

main() {

    char *pstr[10];
    char p[10];
    char *s1 = "morning";
    char s2[10] = {'h','e','l','l','o'};
    char s3[10] = {'g','o','o','d'};
    int i = 0; 

    strcpy(p, s1);
    pstr[0] = p;
    printf("%s\n", pstr[0]);

    strcpy(p, s2);  
    pstr[1] = p;
    printf("%s\n", pstr[1]);

    strcpy(p, s3);  
    pstr[2] = p;
    printf("%s\n", pstr[2]);

    for (i = 0; i < 3; i++)
        printf("%s\n", pstr[i]);
}

The output from the program is:

morning
hello
good
good
good
good
15
  • 2
    Make sure you null-terminate your strings. Commented Jul 11, 2011 at 15:56
  • U-la-la. I suggest you try one(!) thing at a time. When that works you move on to the next, more difficult example. I get dizzy looking at this code, and if someone corrects it for you, you probably still have a hard time to understand....step-by-step. Commented Jul 11, 2011 at 15:57
  • 1
    That output is what I'd expect. What did you expect ? Commented Jul 11, 2011 at 15:59
  • 1
    @Mike Kwan Yes they are. The variable(array) is initialized, but not all members are. Any members of a struct or array not mentioned in an initializer gets their default value (0 for chars). But if the array had no initializers at all, there would be no guarantee. Even char[1024] = "bar"; would guarantee to fill the remaining 1000 elements with 0. Commented Jul 11, 2011 at 16:11
  • 1
    @Mike Kwan No, I am not saying uninitialized variables are assigned 0. int a; , here a is uninitialized. We do not know what it contains. char[10] a; Same here. We don't know what is in a[0] through a[9]. However, with char a[10] = {'x'};, then a is initialized. a[0] will contain x. But, since a[1] through a[9] was not mentioned in the initalizer, they get their default value of 0. Commented Jul 11, 2011 at 16:21

6 Answers 6

6

You have set pstr[0], pstr[1] and pstr[2] to equal p. And the last thing written into p is the byte sequence "good". So at the end, you are essentially printing p three times.

Sign up to request clarification or add additional context in comments.

Comments

4

All three pointers in the pstr array point to the same memory location (p).

And since you modify the contents of that memory location a few times using the strcpy calls, it will contain whatever was placed there last.

In this case, the string "good" was placed there last, so that's what all three pointers in the pstr array will point to, and that's what will get displayed repeatedly in the loop.

Comments

3

Fundamental point - the array of memory referenced by p can only contain one string at a time.

The loop at the end of your code will always print out the same value on each iteration, since you seeded each entry in the array pstr with the same pointer p.

If you want to see different results on each iteration, you have to point pstr[0], pstr[1] and pstr[2] to different areas of memory.

Comments

2

pstr[0], pstr[1], and pstr[2] all point to the same 10-character array p. When you copy each string, you change the contents of the array p, but it's address doesn't change. So, after you copied the last string into p, you just end up printing the same thing three times.

Comments

2

not only all the entries of the array are p, but you are strcpy'ing non zero terminated strings. strcpy() needs the strings to be zero terminated or you'll get a buffer overrun with unpredictable consequences.

Comments

1

Simple, you set pstr[i]=p; bit p is the address of a static buffer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.