×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

structure that contains a pointer to a char

structure that contains a pointer to a char

structure that contains a pointer to a char

(OP)
Hello,

I have a structure that contains a pointer. I am wondering how i can set the structure item *comment and display the contents of *comment.

Currently I am getting a error message: Segmentation fault (core dumped)

Many thanks for any suggestions,

CODE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Records
{
    char name[20];
    char city[20];
    char phone[20];
    char *comment;    
};

struct Records *ptrRecord;

int main(int argc, char** argv)
{        
    ptrRecord = (struct Records *) malloc(2 * sizeof(struct Records));
    char name[20];

    strncpy(name, "Mickey Mouse", sizeof(name));

    strncpy(ptrRecord->name, "Minne",sizeof(ptrRecord->name));
    strcpy(ptrRecord->comment, name);
    
    printf("Name: %s\n", ptrRecord->name);
//    printf("Comment: %s\n", ptrRecord->comment);
        

    return 0;
}

RE: structure that contains a pointer to a char

I think all the statement

CODE

    ptrRecord = (struct Records *) malloc(2 * sizeof(struct Records));

does is allocate storage for the elements of the structure. It doesn't allocate any storage for the char array that comment is supposed to point to. You need to subsequently initialize comment something like

CODE

    for ( i=0; i<2; i++)
    {
     ptrRecord[i].comment = (char*) malloc(20*sizeof(char));
    }
.
.
.

    for ( i=0; i<2;i++)
    {
     free(ptrRecord[i].comment);
    }
    free(ptrRecord);

When I run the program with these changes it produces

Name: Minne
Comment: Mickey Mouse

Hope this helps you.

Good Luck,

Greg

RE: structure that contains a pointer to a char

I don't see any reason why the Record needs to be malloc'ed in the first place, or why you're allocating space for 2 of them?

CODE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Records
{
    char name[20];
    char city[20];
    char phone[20];
    char *comment;    
};


int main(int argc, char** argv)
{
    char comment[20];
    struct Records record;

    strncpy(record.name, "Mickey Mouse", sizeof(record.name));

    strncpy(comment, "Minne", sizeof(comment));
    record.comment = malloc( strlen(comment) + 1 );
    strcpy(record.comment, comment);

    printf("Name: %s\n", record.name);
    printf("Comment: %s\n", record.comment);
        

    return 0;
}

RE: structure that contains a pointer to a char

(OP)
Hello,

Thanks for the reply.

The program worked ok, but I have a few questions.
Because I have created a pointer to the structure, I thought that you had to use the arrow operator to access the items in the structure. i.e.

CODE

ptrRecord->comment
or

CODE

(*ptrRecord).comment
//Dereference first

So I am a little confused about using the dot operator.

CODE

ptrRecord.comment
Many thanks for your help,

CODE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Records
{
    char name[20];
    char city[20];
    char phone[20];
    char *comment;    
};

struct Records *ptrRecord;

int main(int argc, char** argv)
{    
    int i = 0;
    
    ptrRecord = (struct Records *) malloc(2 * sizeof(struct Records));
    
    //Allocate storage on the heap for the comment field.
    for(i = 0; i < 2; i++)
    {
        ptrRecord[i].comment = (char *) malloc(2 * sizeof(char));
    }
    
    char name[20];
    strncpy(name, "Mickey Mouse", sizeof(name));

    strncpy(ptrRecord->name, "Minne",sizeof(ptrRecord->name));
    strcpy(ptrRecord->comment, name);
    
    printf("Name: %s\n", ptrRecord->name);
    printf("Comment: %s\n", ptrRecord->comment);
        

    return 0;
}

 

RE: structure that contains a pointer to a char

(OP)
Hello cpjust,

I was just experimenting as I have not done C programming for a long time. And need to get back into it again.

Steve

RE: structure that contains a pointer to a char

If your struct is a pointer then you use the -> operator to get at the elements, but if it's a regular stack-based variable, you use the . operator.

RE: structure that contains a pointer to a char

Is this

CODE

strncpy(comment, "Minne", sizeof(comment));
correct approach? The statement copies characters outside of the constant string "Minne". Couldn't it lead to segmentation errors too in case when the string is the last one in the code section?

RE: structure that contains a pointer to a char

You never allocate memory space for character string you want comment to point to, only a place to store the pointer to the memory that you haven't allocated.  No wonder things crash for you.

Lee

RE: structure that contains a pointer to a char

Sorry, I missed the post where you did that.

Lee

RE: structure that contains a pointer to a char

Gotta read this stuff in more detail.

CODE

strcpy(ptrRecord->comment, name);
copies a 20-character string to memory you've allocated for 2 characters.

Lee

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close