# Pointers and structs confusing me

• (3 Pages)
• 1
• 2
• 3

## 30 Replies - 649 Views - Last Post: 04 February 2018 - 06:03 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=409110&amp;s=aa5b272d8f9e566395508c3a2f72491d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

# Pointers and structs confusing me

Posted 04 February 2018 - 10:13 AM

/*
float _data;
};

struct CStackL{
int   _size;
};
*/
void   CStackLPush(CStackL *pDettaObjekt, float data){
pDettaObjekt->_size += 1; // lägg till 1 storlek

printf("\n%d\n", (*(*(*pDettaObjekt)._pTop)._pNext));
(*(*(*pDettaObjekt)._pTop)._pNext) = (*(*pDettaObjekt)._pTop); //ta top o lägg det på next
(*pDettaObjekt)._pTop = (struct Link*) malloc(sizeof(struct Link*)); // skapa en minnesplat för adressen på heapen och lägg den på top
(*pDettaObjekt)._pTop->_data = data; // spara data på data
}

im not able to assign pTop to the pNext pointer. I've tried most combinations of * and & to make it work but its just so king confusing for what is the pointer what is the value at the pointer etc. i think the data row is correct but i just can't for some reason get the pNext adress

Is This A Good Question/Topic? 0

## Replies To: Pointers and structs confusing me

### #2 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 10:57 AM

void   CStackLPush(CStackL *pDettaObjekt, float data){
pDettaObjekt->_size += 1; // lägg till 1 storlek
printf("\n%d\n", (*pDettaObjekt)._pTop);
(*(*pDettaObjekt)._pTop)._pNext = (*pDettaObjekt)._pTop; //ta top o lägg det på next
(*pDettaObjekt)._pTop = (struct Link*) malloc(sizeof(struct Link*)); // skapa en minnesplat för adressen på heapen och lägg den på top
(*pDettaObjekt)._pTop->_data = data; // spara data på data
}

this should work. is there a problem because at the start nothing is in pNext?

### #3 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:25 AM

i know it has to do with
(*(*pDettaObjekt)._pTop)._pNext
code. because when i try to print that it doesn't print anything at all. it just gets stuck

### #4 jimblumberg

Reputation: 5388
• Posts: 16,809
• Joined: 25-December 09

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:25 AM

Why should it work? What is your compiler telling you about the problems?

You're only passing one pointer into the function so why are you trying to throw all of those pointers into the mix?

You seem to know how to use the pointer operator->, I suggest you start using it.

What are you trying to print in that printf() function? How can the compiler know how to print a _pTop object? And do you realize that a _pTop is not an integer?

Just throwing pointers at the problem with really knowing what they mean is never going to produce your desired results.

Jim

This post has been edited by jimblumberg: 04 February 2018 - 11:26 AM

### #5 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:27 AM

yah, its a push function for a stack. i want the old pointer to point to next and the new pointer to point to top and then save the value at data. and increment the size by 1

### #6 jimblumberg

Reputation: 5388
• Posts: 16,809
• Joined: 25-December 09

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:35 AM

Quote

yah, its a push function for a stack.

Obviously.

Quote

i want the old pointer to point to next and the new pointer

Okay but this doesn't answer any of the questions I posed.

Why all the pointers?

What are you trying to print with that printf()?

Quote

and increment the size by 1

This is about all you've gotten correct so far.

Jim

### #7 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:39 AM

pDettaObjekt->_size += 1; // lägg till 1 storlek
pDettaObjekt->_pTop->_pNext = pDettaObjekt->_pTop; //ta top o lägg det på next
pDettaObjekt->_pTop = (struct Link*) malloc(sizeof(struct Link*)); // skapa en minnesplat för adressen på heapen och lägg den på top
pDettaObjekt->_pTop->_data = data; // spara data på data

this is how i had it first. still gives me the same results. nothing not even a error. the code just gets stuck at the second row. i know it has something to do with the _pNext pointer i just can't figure out what it is. I've put prints and i know at what row the code stops executing

that print function was just a test to see where the code fails to execute and for what reason. I'm using one pointer to point to the main struct. then one pointer to the top stack and one pointer for the next position in the stack

### #8 jimblumberg

Reputation: 5388
• Posts: 16,809
• Joined: 25-December 09

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:43 AM

What is the point of that malloc?

Quote

I've put prints and i know at what row the code stops executing

Well from your examples the prints won't tell you much since they're not printing anything. Look at this printf() from above:

printf("\n%d\n", (*pDettaObjekt)._pTop);

What exactly are you trying to print with this statement?

By the way trying to debug this type of code with print statements is almost impossible, you need to start using your debugger.

Jim

### #9 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:46 AM

The malloc is for creating a space in the heap memory for the top position. then my pop function just frees that memory later in the code if i can get there. I'm printing and trying to get the adress to the memory but not being able to get it.

### #10 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:53 AM

I'm running the debugger and I'm not seeing _pNext anywhere in the values. it should be in _pTop

### #11 jimblumberg

Reputation: 5388
• Posts: 16,809
• Joined: 25-December 09

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 11:57 AM

Quote

The malloc is for creating a space in the heap memory for the top position.

Okay then why are you trying to assign something before you allocate space for that something?

Quote

then my pop function just frees that memory later in the code if i can get there.

I don't care about this since the only thing you've posted is the code for this function let's stick with this function.

Quote

I'm printing and trying to get the adress to the memory but not being able to get it.

How do you know? You're using the wrong format specifier to print an address so you're invoking undefined behavior.

I suggest you provide the smallest possible complete program to illustrate the problem. For this purpose just show how you're calling this function and show all of your include files used.

... Show the include files you need to compile this "simple" program.

float _data;
};

struct CStackL{
int   _size;
};

void CStackLPush(CStackL *pDettaObjekt, double data){
... Show the code for this function.
}

int main()
{
// Show how you're calling this function.
CStackLPush(........);

}

Make sure the code compiles or if it fails to compile show the complete compiler error messages, all of them, exactly as they appear in your development environment.

Jim

### #12 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 12:17 PM

the rest of the code the teacher has written. he calls all those functions and tests that our implementation works. and since others in my class got it working I'm guessing it works.

But i have figured some stuff out and able to locate the Links in the function i think i will be able to figure this out now.
Thanks a lot of the help

### #13 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:19 PM

SOLVED...

void   CStackLPush(CStackL *pDettaObjekt, float data){

pDettaObjekt->_size += 1;

}

here is the solution if anyone is interested

### #14 jimblumberg

Reputation: 5388
• Posts: 16,809
• Joined: 25-December 09

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:41 PM

Why are you casting the return value from malloc?

Jim

### #15 sepp2k

• D.I.C Lover

Reputation: 2536
• Posts: 4,020
• Joined: 21-June 11

## Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:41 PM