Question on passing array arguments to function, and not printing

This is a discussion on Question on passing array arguments to function, and not printing within the C Programming forums, part of the General Programming Boards category; I'v been working on a binary tree problem. I know there are those that only take one variable, prob an ...

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Question Question on passing array arguments to function, and not printing

    I'v been working on a binary tree problem. I know there are those that only take one variable, prob an int or a char. However, my instructions were to pass a struct with addresses, phone numbers etc. My program compiles but perhaps my logic is crap. I believe it lies in the way i pass the array arguments to the insert function.


    My function takes it like this:

    Code:
    
    BST insertnode(BST t, int x,int a,char *array)
    {
    
            if(t==NULL) {
               t=(node*)malloc(sizeof(node));
           if(t==NULL) printf("\n Out of Space !!");
    
          else {
    
               t->info = x;
               t->phone=a;
    	   t->fname[90]=*array;
    
               t->left = t->right = NULL;
    
            }
        }
    
    
    
        else
          if (x<t->info) {
             t->left=insertnode(t->left,x,a,array);
        }
    
        else
            if (x>t->info) {
               t->right=insertnode(t->right,x,a,array);
        }
    
        return t;
    
    }
    Everything seems to work fine, but when i try to print my information, i get the info and phone to the screen, but the fname doest print anything, which i assume is a problem the way im passing it to the function. ANyway any comments, or help would be appreciated.
    Last edited by Matus; 11-21-2008 at 10:48 AM.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    BST insertnode(BST t, int x,int a,char *array)
    {
    
            if(t==NULL) {
               t=malloc(sizeof(node));
           if(t==NULL) printf("\n Out of Space !!");
    
          else {
    
               t->info = x;
               t->phone=a;
               strncpy(t->fname, array, sizeof(t->fname))
    
               t->left = t->right = NULL;
            }
        }
        else
          if (x<t->info) {
             t->left=insertnode(t->left,x,a,array);
        }
    
        else
            if (x>t->info) {
               t->right=insertnode(t->right,x,a,array);
        }
    
        return t;
    
    }

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you want this line
    Code:
    t->fname[90]=*array;
    to do? I would think that it would set the ninetieth character of fname to whatever the value of the char pointer passed in was, cast to a character. If you want to copy a string, you should use strcpy.

  4. #4
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    [code]
    Dammit how could i have overlooked that assignment that i did, u sure are the master Matt, lol, that worked perfectly!! U are the man, lol!! Thanks bro!!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    What do you want this line
    Code:
    t->fname[90]=*array;
    to do? I would think that it would set the ninetieth character of fname to whatever the value of the char pointer passed in was, cast to a character. If you want to copy a string, you should use strcpy.
    Yea thanks Tabstop, case closed, i overlooked that assignment, wasnt thinking there, Matt solved the prob! Yea and u are right too, strcpy to copy too, that ota work too

    Cheers thanks!
    Last edited by Matus; 11-21-2008 at 10:45 AM.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > t->fname[90]=*array;
    Is fname an array?
    If not, you need to allocate space.

    Then you need to use strcpy()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Salem View Post
    > t->fname[90]=*array;
    Is fname an array?
    If not, you need to allocate space.

    Then you need to use strcpy()
    Yup fname is an array in the type struct Salem, yea strcpy works that was my prob. Thanks man!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Matus View Post
    Dammit how could i have overlooked that assignment that i did, u sure are the master Matt, lol, that worked perfectly!! U are the man, lol!! Thanks bro!!
    Yeah no problem dude. Its a simple mistake that can happen to the best of us. That is why they put erasers on pencils and delete keys on keyboards (in fact, that is why there are 3 keys capable of deletion on a keyboard )

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21