Passing structure pointer within structures

K

kunal s patel

Hi,
I am new to C programming. Here is what I am trying to do

typedef struct A {
char *a;
}A;

struct B {
int b;
A *asample;
};


void main()
{
struct B bk;
}

Now I want to allocate Hello to bk->asample->a. How do I proceed here. Any
help will be appreciated

Thanks,
Kunal
 
L

Leo Violette

If this is 'C' as opposed to C++, I think it will be something like this:
bk->asample = (A*)malloc(A);
bk->asample->a = (char*)malloc(somestringsize);
strcpy(bk->asample->a, "this is my string");

This is off the top of my head. I haven't done C and mallocs in twenty
years.
 
L

Leo Violette

If this is 'C' as opposed to C++, I think it will be something like this:
bk->asample = (A*)malloc(A);
bk->asample->a = (char*)malloc(somestringsize);
strcpy(bk->asample->a, "this is my string");

This is off the top of my head. I haven't done C and mallocs in twenty
years.
 
D

David Wilkinson

kunal said:
Hi,
I am new to C programming. Here is what I am trying to do

typedef struct A {
char *a;
}A;

struct B {
int b;
A *asample;
};


void main()
{
struct B bk;
}

Now I want to allocate Hello to bk->asample->a. How do I proceed here. Any
help will be appreciated

Kunal:

Please don't multi-post. This is not a question about the .NET language C++/CLI
so it does not belong here. The group

microsoft.public.vc.language

is more suitable.
 
D

David Wilkinson

kunal said:
Hi,
I am new to C programming. Here is what I am trying to do

typedef struct A {
char *a;
}A;

struct B {
int b;
A *asample;
};


void main()
{
struct B bk;
}

Now I want to allocate Hello to bk->asample->a. How do I proceed here. Any
help will be appreciated

Kunal:

Please don't multi-post. This is not a question about the .NET language C++/CLI
so it does not belong here. The group

microsoft.public.vc.language

is more suitable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top