Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer assignment.

Status
Not open for further replies.

denc4

Programmer
Feb 17, 2005
107
NL
hi
what i would like to do is store the address contained in a pointer variable at a target address pointed at by another pointer.
Code:
typedef struct STRUCTURE
{
  char name[30];
  struct STRUCTURE *Next;
}DATA;

main()
{
  DATA *p0;
  DATA *p1;
  p0 = p0->Next;

  p0 = p1;        /* alt1: overwrites pointer, not target address */
  *p0 = p1;       /* alt2: error. type mismatch */
  *p0 = *p1;      /* alt3: moves data p1 points to, to where p0 points to */
}
i have played around abit with the indirection character, which resulted in the alternatives above. alternative2 is what i want to achieve, but the compiler complains and rejects it.

any idea how to do this properly?
thx.
 
Code:
p0 = p0->Next;
Since you haven't allocated any memory for p0 yet, this code will crash.


Code:
*p0 = p1;
This makes no sense. You're basically trying to put a square peg into a round hole.

Maybe if you explained what you are trying to do (instead of how you're trying to do it) we can figure out a better way for you to write it.
 
i know i haven't allocated any memory for it in my example above. The reason for this is that the actual code is quite large so i made a version for this forum which illustrates my problem in a succinct form.

i explained what i am trying to achieve in the first sentence of my first post.

my program manages a single linked list. after p0 = p0->Next, p0 becomes a pointer to a pointer within my linked list. now i want to store the value of a pointer (p1) at the offset p0 points to.

i thought this could be done by using the indirection character on p0 in an assignment:

*p0 = p1;

but the compiler says its a type mismatch. the question is; how do i tell the compiler p0 points at a value compatible with p1?
 
You've said p1 is a pointer and we can see that p0 is a pointer to a structure (which contains at least one element called Next, probably also a pointer to a similar structure).

When you dereference p0 (as in *p0) you have a structure on the lefthand side of your assignment not a pointer so it is incompatible with p1 which is a pointer not a structure.

If both p0, p1 and Next are pointers to structures they can each have either of the other assigned to them, i.e.

p0 = p1
p0 = p0 -> Next
p0->Next = p1
p1->Next = p0

In this context your phrase "at the offset p0 points to" is difficult to interpret: it points to a structure, or a location in memory, not an "offset".

==========================================
toff.jpg
Some cause happiness wherever they go; others whenever they go.
 
The only valid things you can do are copy one pointer to another pointer, or copy one structure to another structure. For example:
Code:
/* Copy pointers to pointers. */
p0 = p1;
p0 = p0->Next;
p0->Next = p1;
p0->Next = p1->Next;

/* Copy structures to structures. */
*p0 = *p1;
*p0 = *(p0->Next);
*(p0->Next) = *p1;
*(p0->Next) = *(p1->Next);
 
i've tried to cast the receiving pointer:

(DATA*)p0 = p1;

i had hopes it would accept it, but alas.

i found a way around the problem. i have restructered my code so it won't have to do this assigment statement anymore, it's a bit longer now, but it works.

thanks for the help.
 
There's no casting required; that'll just make things worse.

As mentioned, your problem is that you declared the variable p0 to have the wrong type.

You declared it as a DATA*, which is the address of a DATA. From your description, you don't want a DATA*; you want a DATA**, which is the address of the address of a DATA.
 
I thought he wanted to copy a pointer into an object?

denc4 said:
Code:
*p0 = p1;       /* alt2: error. type mismatch */
alternative2 is what i want to achieve, but the compiler complains and rejects it.
 
I'm judging by this statement:
what i would like to do is store the address contained in a pointer variable at a target address pointed at by another pointer.

If I'm reading that correctly, I believe he expects [tt]*p0 = p1[/tt] to "store the address contained in a pointer variable [i.e. the value of p1] at a target address pointed at by another pointer [i.e. into *p0]."

If that's the case, then p0 needs to be a DATA** so it can contain the address of a pointer, as in:
p0 becomes a pointer to a pointer within my linked list

I may certainly be reading it incorrectly.
 
i mentioned that after p0=p0->Next, p0 is a pointer to a pointer, but now i think i was wrong.
all it did was copy the value of one pointer to another.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top