LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2014, 05:16 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
passing pointer arguments correctly


I'm still slowly trying to understand pointers, and am confused as to why the correct 'head' parameter is not being passed to length() in the following code for an attempted 'linked list'. Hope someone can explain where I'm erring.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 3
struct node 
{
int data;
struct node* next;
};
struct node* head;
   //prototypes
int length(struct node*);
struct node* push(struct node*, int);

int main(void)
{
struct node* head = NULL;

for(int i=1;i<=MAX;i++)
{
push(head, i);

}
//int len = length(head);

}

struct node* push(struct node* head, int a)
{

struct node* newNode;
newNode = malloc(sizeof(struct node));
head = newNode;
newNode->data = a;
newNode->next = NULL;
    //function call
int len = length(head);
printf("count is %d\n",len);
return head;
}

int length(struct node* current)
{
int count = 0;
current= head;
while(current!= NULL)
    {
    count++;
    current = current->next;
    }
    return count;
}
If struct node* head is global, shouldn't it be passed properly to length() in my code?
 
Old 05-18-2014, 05:37 AM   #2
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
You seem to have two variables called head: a global one (line 9) and one that's declared in your main() function (line 16). In main(), you're passing the local one to your other functions.

You might want to sort the indentation in your code out - it would make it easier to read.
 
1 members found this post helpful.
Old 05-18-2014, 05:59 AM   #3
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Thanks for that quick analysis. That should help my understanding greatly. Now, at least, the counter is counting (to one)!
 
Old 05-18-2014, 06:30 AM   #4
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
Think about whether you really need a global. It will confuse things, like in this case where you have several variables with the same name. There are probably other reasons too.
 
1 members found this post helpful.
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH - passing arguments Garrett85 Programming 4 01-09-2013 03:03 PM
[SOLVED] Passing input arguments by reference in C. stf92 Programming 4 08-19-2011 08:24 PM
passing arguments to a child process in C++ bhanuvrat Programming 7 02-07-2010 10:01 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 01:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration