C/Pointers & structure
Expert: Narendra - 10/15/2007
QuestionQUESTION: 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.