Thread: Pointer array is not working with me

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    2

    Pointer array is not working with me

    Code:
    
    
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main(){
        int num_players;
    
    
        char example[10];
    
    
        char *players_array[3];
        char *cerial[2] = {"apple", "Cinammon"};
    
    
    printf("How many players are playing?");
    scanf("%d", &num_players);
    getchar();
    int x = 0;
    int y;
    
    
    
    
    
    
    
    
    for(x = 0; x<3; x++){
    fgets(example, 10, stdin);
    example[strlen(example)-1] = '\0';
    players_array[x] = example;
    
    
    }
    
    
    
    
    for(x = 0; x <3; x++){
    printf("\n%s\n", players_array[x]);
    }
    
    
    
    
    
    
    return 0;
    
    
    }
    
    




    1. When I enter 3, john, blake, david, it prints out david 3 times.
    2. I'm trying to use fgets to get the strings for the pointer array
    What am I doing wrong?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    example is a single array it has only a single address for its start!

    Therefore you only display its value three times.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    2
    Quote Originally Posted by stahta01 View Post
    example is a single array it has only a single address for its start!

    Therefore you only display its value three times.

    Tim S.

    Wow, I feel pretty stupid now lol .
    Is there a way I can get the 3 names and store it in the pointer array?
    I'm using
    Code:
    #include <stdlib.h>#include <stdio.h>
    #include <ctype.h>
    
    
    int main(){
        int num_players;
    
    
        char example[10];
    
    
        char *players_array[3];
        char *cerial[2] = {"apple", "Cinammon"};
    
    
    printf("How many players are playing?");
    scanf("%d", &num_players);
    getchar();
    int x = 0;
    int y;
    
    
    
    
    
    
    
    
    for(x = 0; x<3; x++){
    fgets(players_array[x], 10, stdin);
    
    
    }
    
    
    
    
    for(x = 0; x <3; x++){
    printf("\n%s\n", players_array[x]);
    }
    
    
    
    
    
    
    return 0;
    
    
    }
    and it's not working for me.

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    You have to remember that a pointer in C doesn't have any data, it just tells you where to go to find data.

    And whatever the pointer says, can only hold one thing, unless you have a complex data type (which comes later).

    So if your pointer says, "In apartment #3" then there can only be one person living in apt. 3 at a time!

    So when you do:

    Code:
    fgets(example, 10, stdin);
    // ...
    players_array[x] = example;
    What you are doing is creating an array of three values, where each value (a pointer) says: "in apartment #example".

    So when you print, you are doing:

    Code:
    printf("\n%s\n", (in apartment #example));
    So you're going to get whatever is in apartment #example printed three times.

    If you have learned about malloc(), this would be a good time to use it. Read the data in with fgets(), then clean it up (exactly like you are doing it now -- that's totally correct!) and then use malloc() and strcpy() to copy the cleaned-up names to allocated memory.

    If you have learned about strdup(), this would be a good time to use it. The strdup() function does exactly what I just mentioned -- copies a string into memory it obtains from malloc(). You'll still need to read it in and clean it up, as above.

    If you haven't learned about malloc() or strdup(), then you might want to create 3 different input buffers. You can declare them as example1, example2, and example3, just by copying the line that declares example and editing a bit. But that's not great. If you know about arrays and multi-dimensional arrays you could make example be a 2-d array of char, example[3][10]. Then you could read into example[0], example[1], and example[2] for the three different players.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to Structure not working
    By sangamesh in forum C Programming
    Replies: 4
    Last Post: 12-28-2011, 06:36 AM
  2. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 12:52 AM
  3. Pointer assignment not working
    By coderCPP1981 in forum C Programming
    Replies: 6
    Last Post: 06-20-2008, 06:59 AM
  4. Pointer to function isn't exactly working
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2002, 09:43 AM
  5. i have a pointer that it aint working. plz help.
    By adventurer in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2002, 05:58 PM

Tags for this Thread