Pointers and structs confusing me

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 649 Views - Last Post: 04 February 2018 - 06:03 PM Rate Topic: -----

#1 ben255  Icon User is offline

  • D.I.C Addict

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

Pointers and structs confusing me

Posted 04 February 2018 - 10:13 AM

/*
struct Link {
    float _data;
    Link *_pNext;
};

struct CStackL{
    Link *_pTop;
    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  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5388
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5388
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#7 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 5388
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#9 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon

Reputation: 5388
  • View blog
  • 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.


struct Link {
    float _data;
    struct Link *_pNext;
};

struct CStackL{
    struct Link *_pTop;
    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
Was This Post Helpful? 1
  • +
  • -

#12 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • 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 :D
Was This Post Helpful? 0
  • +
  • -

#13 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • 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;
   struct Link * newLink = (struct Link *)  malloc(sizeof(struct Link*));
   newLink->_pNext = pDettaObjekt->_pTop;
   newLink->_data = data;
   pDettaObjekt->_pTop = newLink;

}


here is the solution if anyone is interested
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon

Reputation: 5388
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:41 PM

struct Link * newLink = (struct Link *)  malloc(sizeof(struct Link*));


This is wrong. sizeof(struct Link*) is the size of a pointer (so 4 on 32-bit platforms and 8 on 64-bit platforms) and that's not enough memory to store a Link struct in it.
Was This Post Helpful? 2
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3