You are here:

C/Pointers & structure

Advertisement


Question
QUESTION: Hello,
1-Can I assign an address to a pointer?
2-How to solve the following.
Ex:
typedef struct
{
int a,
int array1[3];
} test;
int array2[]={0x01,0x02,0x03};
void main()
{
test mystructure;
mystructure.array1=array2;   //error: incompatibe type assigment
}

Remark: I am using GCC

ANSWER: 1-Can I assign an address to a pointer?
ANS: Yes.

2-How to solve the following.
Ex:
typedef struct
{
int a,
int array1[3];
} test;
int array2[]={0x01,0x02,0x03};
void main()
{
test mystructure;
mystructure.array1=array2;   //error: incompatibe type assigment
}

In this program you haven't defined any pointers at all.
And the address of array is fixed. So, you cannot assign the address of one array to another address.
If your array1 was defined as a pointer then your error would have gone.

---------- FOLLOW-UP ----------

QUESTION: hello;

How to assign an address to my pointer.
Let's say I want int* A to be point to address 0x00001234

Thanks, Regards.

Answer
> How to assign an address to my pointer.
> Let's say I want int* A to be point to address 0x00001234
You can do this:
int *A = 0x00001234;

But, since you are giving the address directly, it is your responsibility to make sure that the address 0x00001234 is a valid address.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.