C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2012, 03:15 AM
Cataladu Cataladu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 Cataladu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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=&num;
printf("The new value of myPointer2 is: %d\n",*(myPointer2->data));
myPointer = myPointer2;

printf("The new value of myPointer is: %d\n",*(myPointer->data));

}

Reply With Quote
  #2  
Old October 6th, 2012, 03:37 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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=&num;// WRONG!

Reply With Quote
  #3  
Old October 6th, 2012, 03:54 AM
Cataladu Cataladu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 Cataladu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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=&num; 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.

Reply With Quote
  #4  
Old October 6th, 2012, 03:55 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,701 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 48 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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.

Reply With Quote
  #5  
Old October 6th, 2012, 04:20 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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

Reply With Quote
  #6  
Old October 6th, 2012, 04:20 AM
Cataladu Cataladu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 Cataladu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C Pointers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap