[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

C pointer question

Posted on 2008-01-29
6
Medium Priority
?
212 Views
Last Modified: 2010-04-15
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

0
Comment
Question by:ambuli
  • 3
  • 2
6 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1400 total points
ID: 20769823
>>          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
 

Author Comment

by:ambuli
ID: 20769862
Thanks.
Yes, I tried
value = "somevalue" and it crashed as well.  How can I correct it.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20769876
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 53

Expert Comment

by:Infinity08
ID: 20769893
>> 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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 600 total points
ID: 20769898
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
 

Author Comment

by:ambuli
ID: 20769959
Thanks a lot.  I learnt something today :-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question