C pointer question

Hi there,

The following small code crashes when executing.  What is
typedef struct{
	char *name;
}my_struct;
 
void copy_from(char *value)
{
          //value = "somevalue";  I tried this one with the same problem
          strcpy(value, "somevalue");	
}
 
void copy_another(my_struct *table)
{
	copy_from(table->name);
}
 
int main()
{
	my_struct my;
 
	copy_another(&my);
	printf("%s", my.name );
	
	return 0;
}

Open in new window

ambuliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>>          strcpy(value, "somevalue");

You can't do that since value is a char* and doesn't point to valid memory (yet).

Did you mean :

        value = "somevalue";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambuliAuthor Commented:
Thanks.
Yes, I tried
value = "somevalue" and it crashed as well.  How can I correct it.
0
Infinity08Commented:
Alternatives are :

1) use an array instead of a pointer :

        typedef struct{
                char name[16];
        } my_struct;

    The disadvantage is that there is a maximum size for the name field (and you have to always keep that in mind so you never write past the end of the buffer).


2) allocate some memory for the pointer :

        my_struct my;
        my.name = (char*) calloc(16, sizeof(char));
        copy_another(&my);
        printf("%s", my.name );

    The advantage here is that you can dynamically size the name field.


3) duplicate the string (assuming that strdup is available on your system - otherwise you'll have to implement it) :


typedef struct{
        char *name;
}my_struct;
 
void copy_another(my_struct *table)
{
        table->name = strdup("somevalue");   
}
 
int main()
{
        my_struct my;
 
        copy_another(&my);
        printf("%s", my.name );
        
        return 0;
}

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Infinity08Commented:
>> value = "somevalue" and it crashed as well.

Yes, you can't put it in the copy_from method, since it works on a local copy of the pointer. I should have said that explicitly.
0
jkrCommented:
Or, allocate memory:
#include <stdlib.h>
 
typedef struct{
        char *name;
}my_struct;
 
void copy_from(char *value)
{
          
          strcpy(value, "somevalue");   
}
 
void copy_another(my_struct *table)
{
        copy_from(table->name);
}
 
int main()
{
        my_struct my;
 
        my.name = (char*) malloc(255); // <---
 
        copy_another(&my);
        printf("%s", my.name );
 
        free(my.name);
        
        return 0;
}

Open in new window

0
ambuliAuthor Commented:
Thanks a lot.  I learnt something today :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.