Initializing an Array of Pointers to Char

Discussion in 'C Programming' started by nk, Jul 29, 2006.

  1. nk

    nk Guest

    Hi,
    I'm a newbie on this language. I would be very happy if you help me
    about the following issue:
    The code below, reads some names(strings), stores them, and stores the
    addresses in the pointer array, and writes them out. But it fails and
    exits the program. I guess that it's about initializing the array but i
    couldn't find a way to make it ok.
    --------------------------------------------------------------------------------------------------------------------------------------
    #include <stdio.h>
    #include <conio.h>

    int main()
    {
    char *a[50];

    int i, numb;

    printf("How many entries would you like to make?\n");
    scanf("%d", &numb);

    printf("Enter the names please..\n");

    for ( i = 0; i < numb; i++ ) {
    scanf("%s", a);
    }

    for ( i = 0; i < numb; i++ ) {
    printf("%s", a);
    }

    getch();
    return 0;
    }
    -------------------------------------------------------------------------------------------------------------------------------------

    Thanks..
     
    nk, Jul 29, 2006
    #1
    1. Advertising

  2. nk

    jacob navia Guest

    nk a écrit :
    > Hi,
    > I'm a newbie on this language. I would be very happy if you help me
    > about the following issue:
    > The code below, reads some names(strings), stores them, and stores the
    > addresses in the pointer array, and writes them out. But it fails and
    > exits the program. I guess that it's about initializing the array but i
    > couldn't find a way to make it ok.
    > --------------------------------------------------------------------------------------------------------------------------------------
    > #include <stdio.h>
    > #include <conio.h>
    >
    > int main()
    > {
    > char *a[50];
    >
    > int i, numb;
    >
    > printf("How many entries would you like to make?\n");
    > scanf("%d", &numb);
    >
    > printf("Enter the names please..\n");
    >
    > for ( i = 0; i < numb; i++ ) {
    > scanf("%s", a);
    > }
    >
    > for ( i = 0; i < numb; i++ ) {
    > printf("%s", a);
    > }
    >
    > getch();
    > return 0;
    > }
    > -------------------------------------------------------------------------------------------------------------------------------------
    >
    > Thanks..
    >



    The problem is here

    > char *a[50];


    Here you reserve space for 50 POINTERS to char, that
    at the start of the program point nowhere. You have
    failed to make those pointers point to somewhere.

    Before getting the user's input, allocate space
    for those 50 lines and make those 50 pointers point to the
    allocated space (Hint: malloc())

    After you are done with those pointers, free the allocated space
    (Hint free())

    jacob
     
    jacob navia, Jul 29, 2006
    #2
    1. Advertising

  3. nk

    Comcast News Guest

    [This followup was posted to comp.lang.c]

    While wandering through cyberspace on 29 Jul 2006 11:48:44 -0700, nk
    said ...
    > Hi,
    > I'm a newbie on this language. I would be very happy if you help me
    > about the following issue:
    > The code below, reads some names(strings), stores them, and stores the
    > addresses in the pointer array, and writes them out. But it fails and
    > exits the program. I guess that it's about initializing the array but i
    > couldn't find a way to make it ok.
    > --------------------------------------------------------------------------------------------------------------------------------------
    > #include <stdio.h>
    > #include <conio.h>
    >
    > int main()
    > {
    > char *a[50];
    >
    > int i, numb;
    >
    > printf("How many entries would you like to make?\n");
    > scanf("%d", &numb);
    >
    > printf("Enter the names please..\n");
    >
    > for ( i = 0; i < numb; i++ ) {
    > scanf("%s", a);
    > }
    >
    > for ( i = 0; i < numb; i++ ) {
    > printf("%s", a);
    > }
    >
    > getch();
    > return 0;
    > }
    > -------------------------------------------------------------------------------------------------------------------------------------


    You need to initialize the array of pointers before you use it.

    There are several ways to do this. You can either initialize each
    element of the array to a the result of a call to malloc() or assign the
    address of a static area to each element of the array.
     
    Comcast News, Jul 29, 2006
    #3
  4. nk

    Flash Gordon Guest

    nk wrote:
    > Hi,
    > I'm a newbie on this language. I would be very happy if you help me
    > about the following issue:
    > The code below, reads some names(strings), stores them, and stores the
    > addresses in the pointer array, and writes them out. But it fails and
    > exits the program. I guess that it's about initializing the array but i
    > couldn't find a way to make it ok.
    > --------------------------------------------------------------------------------------------------------------------------------------
    > #include <stdio.h>
    > #include <conio.h>


    Why include conio.h? It's a non-standard header and there are better
    ways of ensuring you see the output of your program than calling getch.

    > int main()


    Better to be explicit about not taking parameters.
    int main(void)

    > {
    > char *a[50];
    >
    > int i, numb;
    >
    > printf("How many entries would you like to make?\n");
    > scanf("%d", &numb);


    What of the user types "one" instead of "1"? Always check the return
    value of input functions and the *scanf family of functions. Although in
    general it is easier to read the input with fgets (NOT gets, NEVER gets)
    and then parse it.

    > printf("Enter the names please..\n");
    >
    > for ( i = 0; i < numb; i++ ) {
    > scanf("%s", a);


    Don't use a %s in scanf without specifying an upper limit. It's like
    holding a shot glass over your computer, giving the user a hose,
    blindfolding him/her so s/he does not know how large the glass is, and
    then saying pour in as much water as you want. At some point someone
    will flood your computer completely wrecking it. gets has *exactly* the
    same problem as the line above.

    You have the additional problem that a is a pointer to char which
    does not point anywhere in particular yet. So scanf will write to some
    random part. scanf does not magically create space for the string it is
    reading, you need to do that before you call it. This also means you
    need to decide on an amount of space and tell scanf how much space
    you've given it.

    Further, you are not checking that the user has input a number <= 50.
    Having fixed all the other problems, the user could still enter 51 and
    blow up your computer.

    > }
    >
    > for ( i = 0; i < numb; i++ ) {
    > printf("%s", a);
    > }
    >
    > getch();


    On my system that function sends insulting messages to the Chinese
    embassy. You could use a standard input function or find out how to get
    your environment not to close the window as soon as the program terminates.

    > return 0;
    > }
    > -------------------------------------------------------------------------------------------------------------------------------------
    >
    > Thanks..


    All my comments are to help you learn even though many of them don't
    address your immediate problem.
    --
    Flash Gordon
    Still sigless on this computer
     
    Flash Gordon, Jul 29, 2006
    #4
  5. nk

    nk Guest

    jacob navia, Comcast News and Flash Gordon, thank you so very much!
    i've almost fixed the problem by your help. it's great to take a
    helping hand when you're a newbie. thanks a lot.
    Flash Gordon, could you please give some examples (alternatives) about
    that issue:

    "Why include conio.h? It's a non-standard header and there are better
    ways of ensuring you see the output of your program than calling
    getch."

    also, it would be very helpful if you inform me a little more about
    specifying an upper limit for scanf when using a %s.

    Thanks a lot !..
     
    nk, Jul 30, 2006
    #5
  6. On 2006-07-30, nk <> wrote:
    > jacob navia, Comcast News and Flash Gordon, thank you so very much!
    > i've almost fixed the problem by your help. it's great to take a
    > helping hand when you're a newbie. thanks a lot.
    > Flash Gordon, could you please give some examples (alternatives) about
    > that issue:
    >
    > "Why include conio.h? It's a non-standard header and there are better
    > ways of ensuring you see the output of your program than calling
    > getch."
    >
    > also, it would be very helpful if you inform me a little more about
    > specifying an upper limit for scanf when using a %s.
    >


    printf ("\n");
    fflush (stdout);

    Either of those will work.
    (Unless you can't see your output because you're in Visual Studio.
    The fix in that case is to stop using Visual Studio.)

    --
    Andrew Poelstra <website down>
    To reach my email, use <email also down>
    New server ETA: 42
     
    Andrew Poelstra, Jul 31, 2006
    #6
  7. Andrew Poelstra said:

    <snip>

    > printf ("\n");
    > fflush (stdout);
    >
    > Either of those will work.
    > (Unless you can't see your output because you're in Visual Studio.
    > The fix in that case is to stop using Visual Studio.)


    No, the fix in that case is to run the program in the debugger, putting a
    breakpoint on the return statement in main() - another great reason to have
    exactly one exit point in your program - or to run it outside the debugger
    but still within Visual Studio (which will say "press any key to continue"
    at the end of the program), or to run it from a console.

    I am far from being the world's greatest Microsoft fan, but there's very
    little wrong with their C compiler and IDE.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 31, 2006
    #7
  8. On 2006-07-31, Richard Heathfield <> wrote:
    > Andrew Poelstra said:
    >
    ><snip>
    >
    >> printf ("\n");
    >> fflush (stdout);
    >>
    >> Either of those will work.
    >> (Unless you can't see your output because you're in Visual Studio.
    >> The fix in that case is to stop using Visual Studio.)

    >
    > No, the fix in that case is to run the program in the debugger, putting a
    > breakpoint on the return statement in main() - another great reason to have
    > exactly one exit point in your program - or to run it outside the debugger
    > but still within Visual Studio (which will say "press any key to continue"
    > at the end of the program), or to run it from a console.
    >
    > I am far from being the world's greatest Microsoft fan, but there's very
    > little wrong with their C compiler and IDE.
    >


    I have nothing against their compiler; I just don't like IDE's in
    general. What I meant by "Stop using VS" was to go into a command line
    and invoke the compiler directly. Or, you could keep using the IDE and
    use a command line to run the program directly. (Either way, it's a good
    experience if only to teach you something about command lines.)

    --
    Andrew Poelstra <website down>
    To reach my email, use <email also down>
    New server ETA: 42
     
    Andrew Poelstra, Jul 31, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,307
    Chris Torek
    Apr 13, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    757
  4. davidb
    Replies:
    0
    Views:
    809
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,596
    Default User
    Sep 1, 2006
Loading...

Share This Page