C assign value to char pointer in struct

This is a discussion on C assign value to char pointer in struct within the C Programming forums, part of the General Programming Boards category; Hi, I'm having a problem assigning a value to a pointer in one of my structs This is the struct ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    C assign value to char pointer in struct

    Hi,

    I'm having a problem assigning a value to a pointer in one of my structs

    This is the struct definition
    Code:
    typedef struct student *student_ptr;
    struct student
    {
    	int ID;
    	char *name;
    };
    
    typedef struct node *node_ptr;
    struct node
    {
    	char code[7];
    	char *name;
    	int n_students;
    	struct student students[1000];
    	node_ptr next;
    };
    Here is the code using it:






    Code:
    		
    char name;
    
    scanf("%s",&name);
    
    node->students[n].name = &name;
    where students is an array of student structs inside the node.

    The problem is that assigning name to node->students[n].name works fine, but when I call the function again to do the same thing to another student in the students array, both students have the exact same name.
    I have tried to scanf directly into node->students[n].name but it does not appear to scan, nor does strcpy work.

    I'm pretty sure that the char* in the student definition is always pointing to &name which causes all of those pointers to point to one variable. Is there a way to make each char pointer of each student point to an individual local &name?

    Any help would be much appreciated

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Code:
    char name;
    
    scanf("%s",&name);
    
    node->students[n].name = &name;
    I'm surprised that doesn't segfault... you'll be stuffing however many characters you get into `name', which is a variable that can hold a single char... bound to be bad. Also, strcpy() won't work, because you need to have allocated the memory for the source, which presumably you're making the same as the destination, so it will just copy it into the same buffer, effectively doing nothing.

    Honestly, you might want to look a bit more into C and its string handling - are you coming from a language that does the memory management for you?

    Anyway, what you're safest doing is using fgets() to put input into a buffer. Put it into a static buffer in your function, then strdup() it:

    Code:
    char buf[128];
    char *ret = fgets(buf, sizeof(buf), stdin);
    if (ret == NULL)
        /* deal with error */
    else
        node->students[n].name = strdup(buf);

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Thanks I came from Java
    The compiler has an error with the code you provided:
    Code:
    Implicit declaration of function strdup
    and
    Code:
    Assignment make pointer from integer without cast
    I'm also using the -O -Wall -ansi -pedantic flags in the compiler
    Last edited by mothermole1; 04-18-2011 at 03:51 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by mothermole1 View Post
    The compiler has an error with the code you provided:
    Code:
    Implicit declaration of function strdup
    and
    Code:
    Assignment make pointer from integer without cast
    Ah, I thought strdup() was a C standard function - turns out it's a POSIX/BSD function. You can easily implement it with something like:

    Code:
    #include <stdlib.h>
    #include <string.h>
    
    char *strdup(const char *str)
    {
        char *new_str = malloc(strlen(str) + 1);
        if (new_str)
            strcpy(new_str, str);
        return new_str;
    }

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    I managed to fix the code, I used a scanf into the buf
    Code:
    scanf("%s",buf);

    then called strdup on buf and cast the return as char*, this seemed to clear it up
    Code:
     node->students[n].name = (char*)strdup(buf);
    Thank you very much!
    Last edited by mothermole1; 04-18-2011 at 05:14 PM.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Yes.

    Code:
    #define MAX_NAME 50
    struct student
    {
    	int ID;
    	char name[MAX_NAME + 1]
    };
    scanf("%s", node->students[n].name); /* be sure to limit the input to length MAX_NAME somehow */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct assign values iterate string
    By kaylaneko in forum C Programming
    Replies: 2
    Last Post: 11-16-2010, 09:51 PM
  2. assign a string to char pointer
    By seaking1 in forum C Programming
    Replies: 8
    Last Post: 12-08-2009, 01:54 PM
  3. How to assign ';' to a char?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2008, 03:08 AM
  4. Can't Return/Assign Char Pointer
    By SiliconHobo in forum C Programming
    Replies: 21
    Last Post: 12-16-2007, 09:56 AM
  5. Assign value of multiple char array elements to int
    By 3saul in forum C Programming
    Replies: 3
    Last Post: 02-03-2006, 04:31 AM

Tags for this Thread


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