The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C Pointers
Discuss C Pointers in the C Programming forum on Dev Shed. C Pointers C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 6th, 2012, 03:15 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 3 h 18 m 55 sec
Reputation Power: 0
|
|
|
C Pointers
Hello everybody,
Can someone help me with this code? I really don't know why It's not working. Why the value in *(pointer->data) is not 5?
I have a larger code, I just did this small sample code to illustrate the problem.
Thanks
#include <stdio.h>
#include <stdlib.h>
struct pointerStruct
{
int * data;
};
typedef struct pointerStruct* myPointerStruct;
void pointerFunction(myPointerStruct,int );
int main()
{
myPointerStruct pointer=(myPointerStruct) malloc(sizeof(myPointerStruct));
pointerFunction(pointer,5);
printf("VALUE AFTER FUNCTION: %d\n",(int)*(pointer->data));
return 0;
}
void pointerFunction(myPointerStruct myPointer, int num){
myPointerStruct myPointer2 = (myPointerStruct) malloc(sizeof(myPointerStruct));
myPointer2->data=#
printf("The new value of myPointer2 is: %d\n",*(myPointer2->data));
myPointer = myPointer2;
printf("The new value of myPointer is: %d\n",*(myPointer->data));
}
|

October 6th, 2012, 03:37 AM
|
|
|
|
First: hiding the pointerness of a type behind a typedef is "unsafe". You tend to forget the type is a pointer. It's better to see the * at the proper places in the code.
Your error is you never allocate memory for data (which is a pointer).
When you do
myPointerStruct myPointer2 = (myPointerStruct) malloc(sizeof(myPointerStruct));
you get a valid memory location for pointer. So pointer->data (another different pointer) exists and can be set to whatever you want. However is has not been initialized and its value cannot be used: which is what you do on the next line
myPointer2->data=#// WRONG!
|

October 6th, 2012, 03:54 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 3 h 18 m 55 sec
Reputation Power: 0
|
|
|
The new value of myPointer2 is: 5
The new value of myPointer is: 5
VALUE AFTER FUNCTION: 2293508
That's the output I get, the program doesn't crash and when I do myPointer2->data=# it actually works and everything. I need the pointer in the main to have the same value as myPointer, they're supposed to be the same variable. I don't know why at the end of the function prints 5 just fine and as soon as it goes back to the main it loses all information. It's like I'm passing the variable by value instead of reference.
|

October 6th, 2012, 03:55 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Couple problems:
1. The thing bdb said about typedef-ing a pointer being unsafe is coming back to bite you. myPointerStruct is a pointer, not a struct: sizeof(myPointerStruct) is the size of a pointer, not the size of the struct. General malloc()ing should look like
Code:
type* ptr = (type*)malloc(sizeof(type));
Pay close attention to where the *s are and are not.
2. num will be lost when the function ends. &num is unsafe: if you look at it inside the function you'll be fine, but if you assign that to something and look at it outside the function you'll have problems.
And FYI, when you ask a programming question, especially one that involves why the program acts in certain way, please explain what it is that it's actually doing. Like saying that the new "new value of" statements show the right thing but when it tries the "after function" one it segfaults.
[edit] Like you just did. Thanks.
|

October 6th, 2012, 04:20 AM
|
|
|
Quote: | Originally Posted by requinix General malloc()ing should look like
Code:
type* ptr = (type*)malloc(sizeof(type));
|
I prefer
Code:
type *ptr = malloc(sizeof *ptr);
C is perfectly capable of converting the void* value returned by malloc() to the correct type of the ptr object: the cast is, at best, redundant; and it may hide an error the compiler would have caught otherwise.
Using the object (rather than the type) as "parameter" to the sizeof operator has the advantage that you don't need to change the code in two locations when you change the type of the pointer ... and you can't get it wrong either 
|

October 6th, 2012, 04:20 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 3 h 18 m 55 sec
Reputation Power: 0
|
|
|
I understand. Thank you so much for your help, I actually checked the code again and changed the design. Now it's working.
Thank you!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|