Thread: "Array" of strings

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    "Array" of strings

    I'm very new to C, and am studying pointers and memory allocation atm. It's fairly different from Java, so I'm a bit disoriented about it. Anyways, I have a problem that asks to read the number of students from the user and allocate memory locations that it equal to the number inserted. Here's the code I've done for that:

    Code:
     
    printf("enter the size of array:");
        scanf("%d",&s);
    
    	names =(char*)malloc(s* sizeof(int));
    Then I am asked to write a function that reads a name of a student and then use malloc to allocate exact space as the lenght to the string inserted.

    Here's my code:

    Code:
    void addName(char*names)
    {
        char *name;
    	
    	scanf("%s",name);
        
    	char *copy = (char*) malloc(sizeof(char)*strlen(name));
    
    	strcpy(copy,name);
    
    	strcpy(names,copy);
    
    	}
    the program crashes on me everytime I run it. Also its supposed to return a pointer to the String, but I couldn't figure that out!

    Any hint would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    name is only an unallocated pointer so scanf("%s",name); will do no more than crash your program pretty hard. scanf() does not allocate the space, you have to do that yourself before calling that function
    Code:
    char name[255];
    scanf("%s",name);
    
    or more safely because it does not accept more characters
    from the keyboard that can be put into the buffer.
    
    fgets(name,sizeof(name),stdin);
    The problem with fgets() is that it appends newline '\n' at the end
    Code:
    fgets(name,sizeof(name),stdin);
    name[strlen(name)-1] = 0; // delete '\n'

    in C it is not necessary to typecast the return value of any function that returns void*, like malloc(). And you forgot to add 1 more byte for the string's null terminator. No need for sizeof(char) because sizeof(char) is ALWAYS == 1.

    Code:
    char *copy =  malloc(strlen(name)+1);
    Code:
    strcpy(names,copy);
    why ^^^^ ??? delete that line.

    finally -- the function is supposed to return the pointer ? Then make the return type char*, not void
    Code:
    char* addName(void)
    {
    
      ...
       return copy;
    }
    Last edited by Ancient Dragon; 02-24-2006 at 08:39 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Well you could use a C compiler, rather than a C++ compiler "pretenting to be C"

    char *name;
    scanf("%s",name);
    This fails for the same reason that you're trying to get past anyway, that a pointer doesn't point anywhere unless you malloc some space. So you initially just read your input to some random location.

    char buff[100];
    fgets( buff, sizeof buff, stdin );
    Now do you strlen / strcpy etc

    > char *copy = (char*) malloc(sizeof(char)*strlen(name));
    Two things
    1. C doesn't have mixed declarations and statements (C++ does)
    2. There is no need to cast malloc in C, if you included stdlib.h

  4. #4
    old man
    Join Date
    Dec 2005
    Posts
    90
    > C doesn't have mixed declarations and statements (C++ does)

    C99 supports this; there may be good reason at times for not relying on its implementation on a given compiler, but it's C.

  5. #5
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by eerok
    > C doesn't have mixed declarations and statements (C++ does)

    C99 supports this; there may be good reason at times for not relying on its implementation on a given compiler, but it's C.
    I think gcc does a pretty good job with most of the C99 stuff. I waited a while until I was sure that what I wanted was supported, and then made the switch.

    Of course, I don't have to worry too much about portability in my particular effort, so the usual "Your mileage may vary" will apply.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    > char *copy = (char*) malloc(sizeof(char)*strlen(name));
    Two things
    1. C doesn't have mixed declarations and statements (C++ does)
    2. There is no need to cast malloc in C, if you included stdlib.h
    3. sizeof(char) is always 1, so you can leave it out

    Remeber to free the memory when you're done with it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM