How to transfer an address value without using pointers

H

Horacius ReX

Hi,

I have the following program structure:

main
.....
int A=5;
int* ptr= A; (so at this point ptr stores address of A)
.....
.....
send_data(...)
....
.....

for the function send_data, I need on its body to use the address of
A:
void send_data(...){
.....
.....
char* R=(char*) ptr;
......

but due to the limitations of the compiler (please dont ask me why :))
I can not use pointers here.

At the moment I do not know how to send to the function the address
information, somebody has idea ?

Thanks in advance
 
V

vippstar

Hi,

I have the following program structure:

main
....
int A=5;
int* ptr= A; (so at this point ptr stores address of A) Don't you mean &A?
....
....
send_data(...) send_data?
...
....

for the function send_data, I need on its body to use the address of
A:
void send_data(...){
....
....
char* R=(char*) ptr;
.....

but due to the limitations of the compiler (please dont ask me why :))
I can not use pointers here. Why?
At the moment I do not know how to send to the function the address
information, somebody has idea ?
You can define ptr to belong to the global scope.
 
G

gw7rib

but due to the limitations of the compiler (please dont ask me why :))
I can not use pointers here.

You may well get better answers if you explain why you can't use
pointers. Is it a problem with the compiler or with your understanding
of the compiler?
 
S

santosh

Horacius said:
Hi,

I have the following program structure:

main
....
int A=5;
int* ptr= A; (so at this point ptr stores address of A)

No. You want:

int *ptr = &A;
....
....
send_data(...)
...
....

for the function send_data, I need on its body to use the address of
A:
void send_data(...){
....
....
char* R=(char*) ptr;
.....

This assigns to R the value derived from interpreting the contents of
ptr as a char * value.
but due to the limitations of the compiler (please dont ask me why :))
I can not use pointers here.

At the moment I do not know how to send to the function the address
information, somebody has idea ?

Thanks in advance

If your system is this limited then you might try storing the address in
a appropriate integer object. If your system has them use intptr_t or
uintptr_t.
 
M

Martin Ambuhl

Horacius said:
Hi,

I have the following program structure:

main
.....
int A=5;
int* ptr= A; (so at this point ptr stores address of A)

No, ptr contains the value of A, which is 5.
To store the address of A you need
int *ptr = &A;

I suspect your subsequent problems are related to this basic error.
 
H

Horacius ReX

You may well get better answers if you explain why you can't use
pointers. Is it a problem with the compiler or with your understanding
of the compiler?

it is a grade question, :)
 
H

Horacius ReX

No. You want:

int *ptr = &A;
right!


This assigns to R the value derived from interpreting the contents of
ptr as a char * value.




If your system is this limited then you might try storing the address in
a appropriate integer object. If your system has them use intptr_t or
uintptr_t.

yes, i tried tranferring an int with the address "coded", but dont
know how to use later in the function this address

what do you mean with intptr_t and uintptr_t ?
 
B

Bart

yes, i tried tranferring an int with the address "coded", but dont
know how to use later in the function this address

Something like this?

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

void send(int ptr){
*(int*)ptr = 1201;
}

int main(void) {
int A=0;

send((int)&A);

printf("A=%d\n",A);
}
what do you mean with intptr_t and uintptr_t ?

Special int types guaranteed to be the right size for your pointers.
If you need your code to run on every computer ever made, past,
present and future, then you need to worry about stuff like this.

-- Bartc
 
H

Horacius ReX

Something like this?

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

void send(int ptr){
*(int*)ptr = 1201;

}

int main(void) {
int A=0;

send((int)&A);

printf("A=%d\n",A);

}


thanks! this is really what was looking for !
 
K

Keith Thompson

Martin Ambuhl said:
No, ptr contains the value of A, which is 5.

No, the declaration violates a constraint, so ptr could contain
anything at all assuming the translation unit isn't rejected.

[...]
 
C

christian.bau

Something like this?

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

void send(int ptr){
  *(int*)ptr = 1201;

}

If you compile this for MacOS X 64 bit (and probably some other
operating systems), this is not just likely to crash, it is one
hundred percent guaranteed to crash.
 
B

ban-ubuntu

If you compile this for MacOS X 64 bit (and probably some other
operating systems), this is not just likely to crash, it is one
hundred percent guaranteed to crash.

I think it's just because of the 64 bit address size that can be
bigger than an int; with a size_t it should work (and it does for me
under Linux 64 bit).
 
V

vippstar

thanks! this is really what was looking for !

that code invokes undefined behavior. I've already explained to Bart
why, but he doesn't seem to appreciate my advice.
You can do this:

#include <stdio.h>

int send(char *str);

int main(void) {
char s[512];
sprintf(s, "p", malloc(1024));
send(s);
return 0;
}

int send(char *str) {
void *p;
sscanf(str, "%p", &p);
free(p);
return 0;
}

Or you can use (u)intptr_t.

However, all these examples use pointers in a way or another, I
suspect your homework question was made up by someone who doesn't
really know C.
 
V

vippstar

I think it's just because of the 64 bit address size that can be
bigger than an int; with a size_t it should work (and it does for me
under Linux 64 bit).
No it shouldn't. Use intptr_t or uintptr_t.
size_t could be 2^2^2^2 bits and a pointer 2^2^2^2^2^2^2 bits for all
you know.
 
R

rahul

that code invokes undefined behavior. I've already explained to Bart
why, but he doesn't seem to appreciate my advice.

Its a common trick that I use many a times. Other than the int data
type being unable to contain the pointer value, I am not aware of any
other issues. If (u)intptr_t is used, it should not be causing any
troubles.Pass me the thread on which the undefined behavior for this
scenario has been discussed.
#include <stdio.h>

int send(char *str);

int main(void) {
char s[512];
sprintf(s, "p", malloc(1024));
sprintf(s, "%p", malloc(1024));
 
V

vippstar

Its a common trick that I use many a times. Other than the int data
type being unable to contain the pointer value, I am not aware of any
other issues. If (u)intptr_t is used, it should not be causing any
troubles.Pass me the thread on which the undefined behavior for this
scenario has been discussed.
common trick? Don't you mean, common pitfall?
There are modern architectures where pointers are 64 bits and ints 32
bits, for example.
 
H

Horacius ReX

Or that it's just too steep for him? I'd be inclined
to ignore him and slope off, leaving him to make his own pitch.

sorry, i was joking about the grade,

just mean that this is very difficult for me to handle
 
H

Horacius ReX

thanks! this is really what was looking for !

that code invokes undefined behavior. I've already explained to Bart
why, but he doesn't seem to appreciate my advice.
You can do this:

#include <stdio.h>

int send(char *str);

int main(void) {
char s[512];
sprintf(s, "p", malloc(1024));
send(s);
return 0;

}

int send(char *str) {
void *p;
sscanf(str, "%p", &p);
free(p);
return 0;

}

Or you can use (u)intptr_t.

However, all these examples use pointers in a way or another, I
suspect your homework question was made up by someone who doesn't
really know C.

in the question, pointers can be used wherever BUT in the arguments of
the function
 

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

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top