Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

structure that contains a pointer to a char 1

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
0
0
TH
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;
}
 
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
 
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;
}
 
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;
}

 
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
 
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.
 
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?
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top