problm with pointers.

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi guys a petty doubt here, why can't we assign absolute
address to the pointers in c language, and what is the reason behind different data types for pointers , in fact they poi t the address locations which are randomly located, right?? :!:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
why can't we assign absolute
address to the pointers in c language

Rubbish. You can.

Quote:
what is the reason behind different data types for pointers

Uh? Type safety. (Gogle it.) And to tell the compiler how to handle what it points to. Eg how should the compiler treat

x = y + *p

if it does not know what type of data is at *p ?

Quote:
in fact they poi t the address locations which are randomly located, right??

I really hope they do not point to random locations.

Is this some kind of school assignment?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of course you can assign an absolute address to a pointer.

You can point an apples pointer to an oranges address. Or vice versa. But it is a fairly useless exercise since an apple is different to an orange.

It is extremely unlikely that assigning random addresses is going to be useful to anyone.

But it will keep you well endowed with 'doubts'.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

why can't we assign absolute
address to the pointers in c language

What are you talking about?

char * ptr = (char *) 0x1234;

is completely valid in C and would then let you read/write SRAM address 0x1234.

Of course you may not know what's at address 0x1234 or (in small AVRs) if it's even a valid location to be using.

As for different types. If I did:

long l;
long * ptrl;
char * ptrc;
ptrl = &l;
ptrc = (char *) &l;
*ptrl = 0x12345678;
*ptrc = 0x12345678;

then one would set the long to be 0x12345678 and the other would set it to be 0x78. I'll leave you to figure out why.

Note however that pointing a pointer of the wrong type to a data object is pretty unusual. Usually you'd use a "long *" to point to a long and a "char *" to point to a char.

EDIT: note to self - must learn to type quicker!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you guys, that was not a school assignment @ johan , i came across a statement saying the following invalid :
char *p = 8899
and it also said that
int a;
char *p;
p=&a is invalid
i asked about them,

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

i came across a statement saying the following invalid :
char *p = 8899

Absolutely. The value 8899 is an int, not a char *. You need to cast it explicitly, as eg Cliff did above.

Quote:

and it also said that
int a;
char *p;
p=&a is invalid

Of-course. You can not take the address of an int (a pointer to an int) and assign it to a pointer to a char. See my post above.

Quote:

i asked about them,

No ypu did not. You claimed that an absolute address can not be assigned to a pointer:
Quote:
why can't we assign absolute
address to the pointers in c language

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yaswanth2008 wrote:
thank you guys, that was not a school assignment @ johan , i came across a statement saying the following invalid :
char *p = 8899

use

char *p = (char *)8899;

Quote:

and it also said that
int a;
char *p;
p=&a is invalid

use

char *p = (char *)&a;

Quote:

i asked about them,

All of these constructions are VERY unwise. After all, you have no idea what is at absolute address 8899 on an AVR or Linux or wherever. And it is very unwise to do things that you have 'doubts' about.

An integer is not the same as a char. Consequently it is unwise to mix them up.

I do not know where you found your code examples, but it may be worth shooting the author.

If you come across similar code in the future, I would just reject it. If the author is that cavalier, she has probably been doing other risky things.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char *p = 8899 

That will have been "makes pointer from integer without a cast". So add a cast:

char *p = (char *)8899;

(but as far as I know there is only ONE model of AVR on which that would be valid unless external SRAM is attached)

int a; 
char *p; 
p=&a

This time you are trying to point a char pointer (8 bit target) at an int (16 bit target) so the compiler is rightly saying "this looks very suspicious - you won't get the result you expect if you read/write through *p you know". If you really want to continue again over-ride with a cast:

int a; 
char *p; 
p=(char *)&a

but are you SURE you want to do this?

EDIT: Rats - it wasn't a question of typing speed this time - I got side-tracked into creating an "e-card"

Last Edited: Wed. Jul 7, 2010 - 02:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you johan,, :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@ david, that was given as an example for invalid statements. so, they are obviously unwise, don't get mad at me for , i am still a noob .

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The simple fact is that you can only set a pointer to something the compiler "thinks" is an address. That is part of the compiler trying to keep you from making errors. It does not know that 8899 is an address. By adding the (char *)8899, you are telling the compiler that it is an address.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The simple fact is that you can only set a pointer to something the compiler "thinks" is an address

The other side of that coin is that can be just about anything (if you cast it to what the compiler expects). In:

char *ptr = (char *)N;

N can be 0..65535 in an AVR and depending where in that numeric range it is placed serious havoc may ensue. (A particularly "fun" one being when N=0..31!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isn't the theory that when you add (char *) as a prefix to a numeric value, you have to make a conscious effort to do so? Surely, the idiot-proof mechanism does not yet exist, but one can make it harder and harder for us ordinary idiots to do things by "accident".

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net