How to program "myvariable=address of data structure" in C?

Status
Not open for further replies.

mwmmboy

Full Member level 2
Joined
May 18, 2001
Messages
134
Helped
8
Reputation
16
Reaction score
2
Trophy points
1,298
Activity points
980
Hi,
I am a beginner using C to program microcontrollers.
I have this problem: I have a variable (int, not a pointer ) representing an address and I have a data structure (array of int) .
How can I do the following operation:

myvariable=Address of the data structure


Tx.
 

Re: Pointer issue

I wrote the follwoing code in DevC++. It will automatically run in a dos window.


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main(void)
{
int x, y[50];

x = (int)y;
printf("\nx: %x y: %p", x, y);
getch();

return 0;
}


I initially wrote code to make sure that sizeof(x) = sizeof so that x would be large enough to contain the value of y which is a pointer.
In this case, a DevC++ int is 4 bytes in size and the pointer is 4 bytes in size. always be careful, because on other systems and compilers these can vary. For example, in TurboC++ int's were 16 bits in size and you had far and near pointers which were different.

Anyway, the only real trick is to use a typecast, in this case typecasting the pointer into an integer with the x = (int)y; statement. Conversions like this are done all the time in c programming.
 

Pointer issue

depends on the microcontroller .... if an address variable ie a pointer in your case has the same size as a long int or default int, you can do a typecast as shown in the previous post
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…