Thread: Access to char pointer within the struct problem

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    5

    Access to char pointer within the struct problem

    I am very new to c and write a small program where a struct will be created and assign some information to it.The code:
    Code:
    #include #include #include typedef struct data {  int no;  char *payload;} message;void new_message(message *msg, int no, char *str);int main(int argc, char** argv) {  message msg1, msg2;   char *str1 = "hello", *str2 = "world!";  new_message(&msg1, 1, str1);  new_message(&msg2, 2, str2);  printf("msg1 no: %d\n", msg1.no);  printf("msg1 payload: %s\n", (&msg1)->payload);  printf("msg2 no: %d\n", msg2.no);  printf("msg2 payload: %s\n", (&msg2)->payload);    return 0;}void new_message(message *msg, int no, char *str) {  msg = malloc(sizeof(msg));  msg->payload = malloc(sizeof(char) * (strlen(str)+1) );  strncpy(msg->payload, str, strlen(str));  msg->no = no;  printf("no in function: %d, payload in function: %s\n", msg->no,          msg->payload);}void print_message(void *m) {  message *msg;  msg = (message *) m;  printf("Thread %d owns %s\n", msg->no, msg->payload);}
    The problem is the data is printed correctly within the function where values are assigned to the structure, but not in main function.
    no in function: 1, payload in function: hellono in function: 2, payload in function: world!msg1 no: 1msg1 payload: ���msg2 no: 134519100����������������9�u߃�[^_]��
    How can I fix this problem?Thanks

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    It looks like you have a problem with newlines while pasting the code.
    Here is a reformated version:
    Code:
    #include
    #include
    #include
    
    typedef struct data {
        int no;
        char *payload;
    } message;
    
    void new_message(message *msg, int no, char *str);
    
    int main(int argc, char** argv)
    {
        message msg1, msg2;
        char *str1 = "hello", *str2 = "world!";
    
        new_message(&msg1, 1, str1);
        new_message(&msg2, 2, str2);
        printf("msg1 no: %d\n", msg1.no);
        printf("msg1 payload: %s\n", (&msg1)->payload);
        printf("msg2 no: %d\n", msg2.no);
        printf("msg2 payload: %s\n", (&msg2)->payload);
        return 0;
    }
    
    void new_message(message *msg, int no, char *str)
    {
        msg = malloc(sizeof(msg));
        msg->payload = malloc(sizeof(char) * (strlen(str)+1) );
        strncpy(msg->payload, str, strlen(str));
        msg->no = no;
        printf("no in function: %d, payload in function: %s\n", msg->no, msg->payload);
    }
    
    void print_message(void *m)
    {
        message *msg;
        msg = (message *) m;
        printf("Thread %d owns %s\n", msg->no, msg->payload);
    }
    Now to the problems.

    You define msg1 and msg2 in main.
    This means you have place for two struct message on stack.
    Then you give the address of the struct to a function, this is okay.
    But in the function, you allocate memory and want asign the address to this pointer.
    This will not work because the pointer is a fixed address to an struct in stack.

    One solution is that you declare msg1 and msg2 as a pointer in main.
    Code:
    int main(int argc, char** argv)
    {
        message *msg1 = NULL, *msg2 = NULL;
        char *str1 = "hello", *str2 = "world!";
    
        new_message(&msg1, 1, str1);
        new_message(&msg2, 2, str2);
        printf("msg1 no: %d\n", msg1->no);
        printf("msg1 payload: %s\n", msg1->payload);
        printf("msg2 no: %d\n", msg2->no);
        printf("msg2 payload: %s\n", msg2->payload);
        return 0;
    }
    Second, malloc can fail and you receive a NULL pointer.
    Check allways the returned value of malloc.

    Your function can look like this:
    Code:
    int new_message(message **msg, int no, char *str)
    {
        if (msg && *msg == NULL) {
            *msg = malloc(sizeof(**msg));
            if (*msg == NULL) {
                printf("Ooops!\n");
                return -1;
            }
        }
    
        if ((*msg)->payload == NULL) {
            (*msg)->payload = malloc(strlen(str) + 1);
            if ((*msg)->payload == NULL) {
                printf("Ooops!\n");
                free(*msg);
                return -1;
            }
        }
    
        strncpy((*msg)->payload, str, strlen(str));
        (*msg)->no = no;
        printf("no in function: %d, payload in function: %s\n", (*msg)->no, (*msg)->payload);
        return 0;
    }
    Changes:
    The funktion return an integer, so the caller can check if it is successfull.
    The returned values from malloc will be checked.
    Last edited by WoodSTokk; 08-30-2015 at 01:08 AM.
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    5
    Quote Originally Posted by WoodSTokk View Post
    It looks like you have a problem with newlines while pasting the code....
    Yes. Thanks for help!
    Quote Originally Posted by WoodSTokk View Post
    Now to the problems.You define msg1 and msg2 in main.This means you have place for two struct message on stack.Then you give the address of the struct to a function, this is okay.But in the function, you allocate memory and want asign the address to this pointer.This will not work because the pointer is a fixed address to an struct in stack.
    Does that mean once the struct is placed in e.g. main function, meaning memory is already been created? Or something similar thus the code does not need to call malloc? The code is working now. Thanks again for kindly help.
    Code:
    typedef struct data {  int no;  char *payload;} message;int new_message(message **msg, int no, char *str);int main(int argc, char** argv) {  message *msg1 = NULL, *msg2 = NULL;   char *str1 = "hello", *str2 = "world!";  new_message(&msg1, 1, str1);  printf("msg1 no: %d\n", msg1->no);  printf("msg1 payload: %s\n", msg1->payload);  printf("==========\n");  new_message(&msg2, 2, str2);  printf("msg2 no: %d\n", msg2->no);  printf("msg2 payload: %s\n", msg2->payload);    return 0;}int new_message(message **msg, int no, char *str) {  if(msg && *msg == NULL) {    *msg = malloc(sizeof(**msg));    if(*msg == NULL) {      printf("Can't allocate message\n");      return -1;    }  }  if((*msg)->payload == NULL) {    (*msg)->payload = malloc(strlen(str)+1);    if((*msg)->payload == NULL) {      printf("Can't allocate payload\n");      free(*msg);      return -1;    }  }  strncpy((*msg)->payload, str, strlen(str));  (*msg)->no = no;  printf("no in function: %d, payload in function: %s\n", (*msg)->no,          (*msg)->payload);}
    Don't know why I just can't correctly format the code pasted. Sorry for that.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by dRichard View Post
    Does that mean once the struct is placed in e.g. main function, meaning memory is already been created? Or something similar thus the code does not need to call malloc? The code is working now. Thanks again for kindly help.
    If you make a local variable of your struct type, then yes, memory is already allocated just like it is for any local variable.
    If you make a local variable of a pointer type, then you would separately need to allocate the memory for the structure itself (as creating that local variable only allocates space for the pointer, not the thing pointed at).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    5
    Quote Originally Posted by Cat View Post
    If you make a local variable of your struct type, then yes, memory is already allocated just like it is for any local variable.If you make a local variable of a pointer type, then you would separately need to allocate the memory for the structure itself (as creating that local variable only allocates space for the pointer, not the thing pointed at).
    The explanation is very clear. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer inside a struct - how to access it?
    By farukyaz in forum C Programming
    Replies: 15
    Last Post: 10-15-2011, 12:58 AM
  2. C assign value to char pointer in struct
    By mothermole1 in forum C Programming
    Replies: 5
    Last Post: 04-18-2011, 05:10 PM
  3. Pointer to struct problem
    By gavio in forum C Programming
    Replies: 10
    Last Post: 08-26-2008, 11:14 AM
  4. Struct pointer problem
    By fkheng in forum C Programming
    Replies: 10
    Last Post: 08-08-2003, 10:59 AM
  5. struct/pointer problem?
    By Jase in forum C Programming
    Replies: 17
    Last Post: 05-31-2003, 11:09 AM