473,508 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reference Parameters in C

5 New Member
Reference parameter can they be done in C?

As in:

I have struct:
Expand|Select|Wrap|Line Numbers
  1. typedef struct listNode listNode;
  2. struct listNode {
  3.     struct listNode *forLink;
  4.     struct listNode *bacLink;
  5.     struct listNode *firLink;
  6.     char *info;
  7. };
  8.  
Now I'm trying (trying being the operative word here) to write a Pop() (which obviously has to remove the last Node and return the info of it) function:
Expand|Select|Wrap|Line Numbers
  1. char *listPop(listNode *_node) {
  2.    char *temp;
  3.    listNode *curLink = _node->firLink;
  4.  
  5.    while (curLink->forLink) 
  6.       curLink->forLink;
  7.    temp = curLink->info;
  8.    _node = curLink->bacLink; //THE PROBLEM
  9.  
  10.    return temp;
  11. }
  12.  
Say I call it by:
Expand|Select|Wrap|Line Numbers
  1.    n = listPop(new); //where new is a listNode obviously.
  2.  
I know in C++ I would be sending the _node as a reference parameter thus when I change it here 'new' would also change. Here however if you GDB it _node changes but it doesn't affect 'new'. How can I do that?
Mar 21 '07 #1
1 1627
DeMan
1,806 Top Contributor
I'll assume you are trying to implement a Stack (given the pop() operation). I'm not entirely sure why you need a reference to firLink, but in a well implemented stack that shouldn't be problem....so We'll ignore that for now.

The pop should be independent of which Node you pass in (so you shouldn't have to set to firLink)....If this is the lastNode (ie forLink == NULL) then we set
Expand|Select|Wrap|Line Numbers
  1. tempNode = _node;
  2. _node->bacLink->forLink=NULL;
  3. free(tempNode);
  4.  
otherwise, we move along forLink until we are at the last Node and do the same thing.


The thing here is that you can modify the list, but you can't update the callers pointer (although you could do by passing a pointer to a pointer)
The point is you can update the data Structure, but not the pointer.....

As long as the caller keeps some reference to firstNode (maybe instead of each Node referencing it), you can modify this structure quite easily.....


(I'm also wondering whether
Expand|Select|Wrap|Line Numbers
  1.  n = listPop(new); //where new is a listNode obviously.
  2.  
should read
Expand|Select|Wrap|Line Numbers
  1.  n = listPop(&new); //where new is a listNode obviously.
  2.  
{and good idea to avoid using new, but I know this was just for an example})
Mar 21 '07 #2

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

Similar topics

110
9812
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
39
7592
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
5
1540
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
5
445
by: Greg | last post by:
Hi, I have been unable to resolve this error message: Object Ref not set to an instance of an object. The issue is that I cannot determine which object reference is causing the problem. ...
7
1556
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
0
2093
by: webbsk | last post by:
I keep getting an exception when I call the DataAdapter Update method. I have been trying to figure out what is causing the null reference exception for this code for what seems like forever: ...
1
1899
by: Kevin | last post by:
ASP.NET 2.0 I have code that updates a database from a number of textboxes on a web form. I've had to hard coded references to my web form textboxes. I'd like to know how I can reference them...
29
3622
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
13
3000
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
275
12038
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7063
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...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
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,...
0
4720
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...
0
3211
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...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
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 ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.