Thread: Pointer strings

  1. #1
    Banned
    Join Date
    May 2003
    Posts
    124

    Pointer strings

    #include <stdio.h>

    int main()
    {
    char *Ptr;

    scanf( "%s", &Ptr );
    printf( "%s", &Ptr );

    return 0;
    }

    Why *Ptr can only take only the first 3 characters from the input? When you put more than 3 characters it doesn't work. Why?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why *Ptr can only take only the first 3 characters from the input?
    Because you're calling scanf/printf incorrectly with an uninitialized pointer. This oozes undefined behavior.

    This is a classic and common trap. All pointers must have memory allocated to them either directly by anonymous allocation (malloc/calloc/realloc) or by assignment to named memory:
    Code:
    /* Anonymous allocation */
    char *Ptr = malloc ( BUFSIZ );
    
    /* Assignment to named memory */
    char buff[BUFSIZ];
    char *Ptr = buff;
    >scanf( "%s", &Ptr );
    scanf expects a char*, if your variable is already a pointer, you don't need the address-of operator, otherwise you do.

    >printf( "%s", &Ptr );
    printf's %s flag expects a char*, you're passing a char**. printf is not scanf, remove the address-of operator.
    My best code is written with the delete key.

  3. #3
    Banned
    Join Date
    May 2003
    Posts
    124

    malloc

    Thanks guys.
    Bad what exactly is malloc, and BUFSIZ?

  4. #4
    Banned
    Join Date
    May 2003
    Posts
    124
    This works:

    /* Assignment to named memory */
    char buff[BUFSIZ];
    char *Ptr = buff;

    but this doesn't work:

    /* Anonymous allocation */
    char *Ptr = malloc ( BUFSIZ );

    why?

  5. #5
    Banned
    Join Date
    May 2003
    Posts
    124

    array of strings

    I have another problem:
    I can't make an array of strings using pointers, without knowing its elements in advance.

    I type:
    char buff[10];
    char *Ptr[20] = { buff };

    but it doesn't work.
    What's the right cote?
    Help me pls.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but this doesn't work
    'Doesn't work' is a very vague problem description, can you specify how it doesn't work as opposed to how you expected it to? Perhaps post the code you're using so that we can play with it as well?

    >I can't make an array of strings using pointers, without knowing its elements in advance.
    You're stuck then. At some point you have to specify how many characters are in the string so that you can allocate the appropriate amount of memory. For just a simple array of strings that won't change you can do something like this:
    Code:
    char *Ptr[] = {
        "First",
        "Second",
        "Third",
    };
    However, you can't modify the memory that the pointers refer to because string literals are not modifiable. There are tricky ways to create variable length arrays of variable length strings, but they can get very complex and shouldn't be used unless absolutely necessary. The simplest solution is simply to bite the bullet and create a two dimensional array.
    My best code is written with the delete key.

  7. #7
    Banned
    Join Date
    May 2003
    Posts
    124
    I type:

    char buff[10];
    char *Ptr[20] = { buff };

    but it doesn't work.
    What's the right cote?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char *Ptr[20] = { buff };
    This tries to initialize a pointer variable with the address of a local object. This isn't standard behavior, so it won't work unless your compiler supports an extension. The closest standard version would be
    Code:
    char *Ptr[20];
    
    Ptr[0] = buff;
    This assigns the address of buff to the first element of Ptr and leaves the rest of them uninitialized.
    My best code is written with the delete key.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by AProg
    I type:

    char buff[10];
    char *Ptr[20] = { buff };

    but it doesn't work.
    What's the right cote?
    Stop spamming the board. We've read your post. You haven't read the replies. Do not just say "it doesn't work". That tells me nothing.

    If it doesn't compile, say it doesn't. Then say what it exactly does do. (IE: Error messages.)
    If it doesn't do what you expect, state what you expect it to do, and tell us what it does or doesn't do.

    Be helpful if you wan't help, otherwise, I'll just $$$$$ you out.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Banned
    Join Date
    May 2003
    Posts
    124
    Look:
    The problem is that i can't declare the arrey of pointers. During the execution of the programm, each pointer will get a string.
    Let's suppose that we will get 10 strings consisting of 20 characters.

    What's the code of the declearetion?

  11. #11
    Banned
    Join Date
    May 2003
    Posts
    124
    Thanks.
    But how can i do this using pointers?

  12. #12
    Banned
    Join Date
    May 2003
    Posts
    124
    Thanks again.
    But can u explain me what exaxtly malloc does, and why i have to use it?

  13. #13
    Banned
    Join Date
    May 2003
    Posts
    124
    Before answering malloc:
    I follow your advice and i type:

    int main()
    {
    char names[10][20];
    int i, n;

    scanf( "%d", &n );
    for( i = 0; i<= n-1; i++ )
    scanf( "%s", names[i] );

    for( i = 0; i<= n-1; i++ )
    printf( "%s\n", names[i] );

    return 0;
    }

    It works properly, but prossesor fails and tells me : programm stopped( not source available )
    why?

  14. #14
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It works properly but it fails? Do you mean that it compiles fine, but gives you a runtime error?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  15. #15
    Banned
    Join Date
    May 2003
    Posts
    124
    It prints the right results, but compiler fail sand program terminates( no source available ).
    Why?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. pointer and strings
    By lostpoet in forum C Programming
    Replies: 8
    Last Post: 03-08-2002, 11:51 AM