rashmi said:
Dear All,
Here is the small program that i am trying to do :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct nested_struct
{
char *name;
};
struct some
{
struct nested_struct *test;
};
int main()
{
struct some some_declare;
some_declare.test=(struct nested_struct *)
malloc(sizeof(struct nested_struct));
Better: some_declare.test = malloc(sizeof *some_declare.test);
if(some_declare.test != NULL)
{
strcpy(some_declare.test.name,"weqweqw");
Two problems here. Firstly, some_declare.test is not a struct, but a pointer
to a struct. To get at its member, you need to do this:
some_declare.test->name
Secondly, you haven't allocated any storage in which to store the copy of
the string. So:
some_declare.test->name = malloc(strlen("weqweqw") + 1);
if(some_declare.test->name != NULL)
{
strcpy(some_declare.test->name, "weqweqw");
Don't forget to free(some_declare.test->name) and then
free(some_declare.test) after you're done with them.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)