Arrays of pointers (input)

This is a discussion on Arrays of pointers (input) within the C Programming forums, part of the General Programming Boards category; Hello, why is it that i can't get string literals and asign them to pointer variables with scanf() and printf()? ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Arrays of pointers (input)

    Hello, why is it that i can't get string literals and asign them to pointer variables with scanf() and printf()?

    Code:
    #include <stdio.h>
    
    main()
    {
    	char * name[3]; /* An array of pointers to characters */
    
    	fgets(name[0], 81, stdin); /* Get a string literal */
    	puts(*name);
    
    	/* printf("%s\n", name[0]); why won't this function work? It always returns(NULL) */
    	/* Also, scanf("%s", name[0]); doesnt' work for getting input either */
    
    return 0;
    
    }
    Thanks,
    Tyler

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Might wish to check again. It is all working a ok with my computers.
    Code:
    #include <stdio.h>
    int main(void)
    {
    char * name[3]; /* An array of pointers to strings */
    /*fgets(name[0], 81, stdin);*/
    /*name[0][strlen(name[0])-1]='\0';*/  /*Using to remove the newline character when using fgets */
    scanf("%s", name[0]);
    /*puts(*name);*/
    printf("**%d**", printf("%s\n", name[0]));
    
    return 0;
    }
    Code:
    #include <stdio.h>
    int main(void)
    {
    char * name[3]; /* An array of pointers to strings */
    fgets(name[0], 81, stdin);
    name[0][strlen(name[0])-1]='\0';  /*Using to remove the newline character when using fgets */
    /*scanf("%s", name[0]);*/
    /*puts(*name);*/
    printf("**%d**", printf("%s\n", name[0]));
    
    return 0;
    }
    The the other combos, they all work just fine.

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    not sure but:
    wouldnt you need to allocate memory for the string scanf reads in?


    would

    char * name[3];
    scanf("%s", name[0]);

    just initialise the space for the ptrs not the space for the string
    "Assumptions are the mother of all **** ups!"

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Thanks for your prompt replies!

    ...I'm using the gcc compiler from Delories web site....
    Code:
    C:\>gcc -o new new.c
    new.c:12:2: warning: no newline at end of file
    
    
    
    C:\>new
    
    kj /* This is the characters i pressed */
    (null)
    **7**
    
    C:\>gcc -o new new.c
    new.c:12:2: warning: no newline at end of file
    
    
    
    
    C:\>new
    
    lj /* This is the characters i pressed */
    Exiting due to signal SIGSEGV
    General Protection Fault at eip=00001622
    eax=ffffffff ebx=00000298 ecx=0000000d edx=00000000 esi=00000054 edi=0000f970
    ebp=0008f950 esp=0008f930 program=C:\DOCUME~1\TYLER\DESKTOP\PROGRAMS\NEW.EXE
    cs: sel=01a7  base=02980000  limit=0009ffff
    ds: sel=01af  base=02980000  limit=0009ffff
    es: sel=01af  base=02980000  limit=0009ffff
    fs: sel=017f  base=0000dd70  limit=0000ffff
    gs: sel=01bf  base=00000000  limit=0010ffff
    ss: sel=01af  base=02980000  limit=0009ffff
    App stack: [0008f970..0000f970]  Exceptn stack: [0000f8d0..0000d990]
    
    Call frame traceback EIPs:
      0x00001622
      0x00002e38
    It gaves me this same crap every time.
    Thanks,
    Tyler

  5. #5
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    try this, i havent checked it and i dont know if its right

    Code:
    main()
    {
    char * name[3]; /* An array of pointers to characters */
    
    //allocate memory for strings
    for ( x = 0; x < 3; x++ ) {
     if ( ( name[ x ]   = malloc( sizeof( char ) * 81 ) ) == NULL ) {
          exit( 1 );
      }
    }
    
    scanf( "%s", name[ x ] ); /* Get a string literal */
    
    return 0;
    
    }
    "Assumptions are the mother of all **** ups!"

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    tyskater what version is that gcc compiler?
    Last edited by Thantos; 12-16-2003 at 10:05 PM.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Kinasz,
    your code does the same as mine, thanks though. Maybe it's the compiler.

    Thantos,
    I downloaded the compiler from:
    http://www.delorie.com/djgpp/zip-picker.cgi
    I don't think it has specific versions.
    Thanks,

    Tyler

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    type:
    gcc -v

    and it should output the version.

    One problem is with not allocating enough memory for the strings (bow Kinasz). Try this code out. With full warnings in effect on GCC and running through Splint with only one warning it should work.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char *name[3];
    
      *name = malloc (3 * sizeof (char) * 81);
    
      if ( name == NULL)
      {
        (void)puts("Error getting memory");
        return 1;
      }
    
      (void)fgets(name[0], 81, stdin);
      (void)printf("***%d***\n", printf("%s", name[0]));
    
      free(*name);
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Thanks so much! That's awesome! It worked!


    Yet, i'v never seen this done before:
    Code:
    (void)printf("***%d***\n", printf("%s", name[0]));
    Did you type cast the printf() function as type void or something? And the printf function within the printf function is wierd.


    ..Oh ya, the gcc version is 3.3.2

    Thanks again...
    Tyler

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The type casting is something I did to shut splint up. Since printf returns a value if you don't use it splint wants it casted to (void) so its for sure an ignored value.
    Yea the printf in a printf is a first for me to. But since you mentioned that it was returning NULL I thought of a way to easily check the return value.

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Thanks man.
    Tyler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 10:34 PM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21