473,811 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer's address don't change

7 New Member
I've got code:

Expand|Select|Wrap|Line Numbers
  1. struct Cell{
  2.     list<int> vertices;
  3.     bool discrete;
  4.     Cell* nextCell;
  5. }
  6.  
  7. struct SearchNode{
  8.     Cell* partition;
  9.     SearchNode* nextSearchNode;
  10. }
  11.  
  12. bool isDiscrete(Cell* c){
  13.     while(c!=NULL){
  14.         if(!c->discrete)
  15.             return false;
  16.         c = c->nextCell;
  17.     }
  18.     return true;
  19. }
  20.  
  21. void stabilise(SearchNode* s){
  22.         .......
  23.     Cell* c = s->partition;
  24.     isDiscrete(c);
  25. }
after isDiscrete(c) call, c address never change, it always point to head. Any idea?
Aug 27 '07 #1
7 2436
gsi
51 New Member
HI,

after isDiscrete(c) call, c address never change, it always point to head.
You are passing 'c' to the isDiscrete function as pass by value . So the value of c is actually copied over and used in the isDiscrete function. Although 'c' is a pointer, you can change the value in memory to which 'c' is pointing to , not the value of c itself.

Use pass by reference instead.

it is like,

Expand|Select|Wrap|Line Numbers
  1. void stabilise(SearchNode* s){
  2. isDiscrete(c); // function call.
  3. }
  4. isDiscrete(Cell& * c){
  5. }
  6.  
Thanks,
gsi.
Aug 27 '07 #2
doreply
7 New Member
HI,

Although 'c' is a pointer, you can change the value in memory to which 'c' is pointing to , not the value of c itself.
How can I kown it is passed by value or it is passd by address when the function has parameter as pointer?
Aug 27 '07 #3
gsi
51 New Member
Hi,

isDiscrete(Cell & * c){
}
Sorry, It must have been,

Expand|Select|Wrap|Line Numbers
  1. isDiscrete(Cell*& c){
  2.  
How can I kown it is passed by value or it is passd by address when the function has parameter as pointer
The function parameter may be a pointer, pointer to a pointer ...etc , but the default parameter passing mechanism is Pass by value in (c++) unless you expilicitly do something like the one above to pass by reference or the one below (pass address by value),

Expand|Select|Wrap|Line Numbers
  1. bool isDiscrete(Cell** c){
  2. //
  3. }
  4. return true;
  5. }
  6.  
  7. void stabilise(SearchNode* s){
  8. .......
  9. Cell* c = s->partition;
  10. isDiscrete(&c);
  11. }
  12.  
  13.  
In pass by value, argument's value are copied over to the parameters,
In pass address by value , argument's address are copied over to the parameters.
In pass by reference(C++), parameters become aliases to the arguments (but internally still manipulated using pointers)
Thanks,
gsi
Aug 27 '07 #4
doreply
7 New Member
Thanks! I understand it now. Doreply.
Aug 27 '07 #5
doreply
7 New Member
The other question is:
Expand|Select|Wrap|Line Numbers
  1. extern Node* this_node;
  2. .....
  3.  
  4. struct SearchNode{
  5.   Cell* partition;
  6.   Node* node;
  7.   SearchNode(){
  8.     node=NULL;
  9.     partition=NULL;
  10.     nextSearchNode=NULL;
  11.   }
  12. }
  13.  
  14. void partition(Cell* c){
  15.     ....   // here i used this_node
  16.  
  17. void newFunction(SearchNode* s){  
  18.                          // SearchNode* s = new SearchNode; 
  19.                          // s->partition = ....; 
  20.    Cell* c = s->partition;
  21.    partition(c);
  22.    ......
  23. }
  24.  
I checked the value after run the program:
before partition(c): s->node = NULL, s->nextSearchNo de = NULL
after: s->node is not NULL, s->nextSearchNo de still is NULL.

Why s->node changed?
Aug 27 '07 #6
gsi
51 New Member
Hi,
struct SearchNode{
Cell* partition;
Node* node;
SearchNode(){
node=NULL;
partition=NULL;
nextSearchNode= NULL;
}
}
I dont see nextSearchNode declared inside the structure.

Thanks,
gsi.
Aug 27 '07 #7
doreply
7 New Member
I dont see nextSearchNode declared inside the structure.
That's why I feel strange. I used debugger and used hard code to check it, the debugger: before partition(c): s->node = 00 after s->node = 04. I've checked all the codes. The only suspect place affect the code is: extern Node* this_node which I used in partition function. When I enable this_node in partition function, s->node will change, otherwise, no problem at all. But this_node is irrelavent to SearchNode and SearchNode* s is not in partition function. Very strange!
Aug 28 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
2069
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << " and &s = " << &s << "\n";
22
3298
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
4
1187
by: xuatla | last post by:
Hi, I have a class class myType { private: int size; double *elem; .....
52
5672
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every time I get the textbooks out end up throwing them against the wall in rage. Thats been going on for 10 years now. Anyway, I have: typedef struct _record { int age;
35
2909
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
16
2317
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
204
13142
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
23
7824
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
42
5354
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
6
2326
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the one- after-last thingy) is undefined behaviour. Actually I was trying to associate a function pointer with a key, through an AVL tree that managed void* data. Function pointers can't be stored in void* (that is, the standard does not garantee...
0
10389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9205
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.