Like Tree1Likes
  • 1 Post By oogabooga

retrieving a value from nested struct gives negative values

This is a discussion on retrieving a value from nested struct gives negative values within the C Programming forums, part of the General Programming Boards category; Code: /* * treeattempt1.c * * Created on: 21/06/2012 * Author: logan */ #include <stdio.h> struct node { int value; ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    retrieving a value from nested struct gives negative values

    Code:
    /*
     * treeattempt1.c
     *
     *  Created on: 21/06/2012
     *      Author: logan
     */
    
    #include <stdio.h>
    
    struct node {
    	int value;
    	struct element* elements;
    };
    
    struct element {
    	struct node* child_node;
    };
    
    int numbers[3]={1,2,3};
    
    int main() {
    	// create a tree with three branches
    	struct element branches[3];
    	struct node tree;
    	tree.elements=&branches[0];
    
    	// before we populate tree we need to point each child nodes to root of the tree
    	struct node childs[3];
    	int m=0;
    	for (m=0; m<3; m++) {
    		tree.elements[m].child_node=&childs[m];
    	}
    
    	// fill tree with numbers 1, 2, 3
    	int i=0;
    	for (i=0; i<3; i++) {
    		tree.elements[i].child_node->value=numbers[i];
    	}
    
    	// try printing out values in the tree
    	int j=0;
    	for (j=0; j<3; j++) {
    		printf("%d", tree.elements[i].child_node->value);
    	}
    	return 0;
    }
    This gives three negative values when I expect values 1, 2, 3. Any help will be appreciated!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That's one wacky data structure you've created there. Your error, besides the general insanity, is using i instead of j in your printf.
    King Mir likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    24
    Quote Originally Posted by oogabooga View Post
    That's one wacky data structure you've created there. Your error, besides the general insanity, is using i instead of j in your printf.
    Thanks man. Don't call me wacko though. I was trying to create a tree like structure.

    Fixing that small error it prints 123.

    Code:
    /*
     * treeattempt1.h
     *
     *  Created on: 21/06/2012
     *      Author: logan
     */
    
    #ifndef TREEATTEMPT1_H_
    #define TREEATTEMPT1_H_
    
    
    #endif /* TREEATTEMPT1_H_ */
    
    struct node {
    	int value;
    	struct element* elements;
    };
    
    struct element {
    	struct node* child_node;
    };
    Code:
    /*
     * treeattempt1.c
     *
     *  Created on: 21/06/2012
     *      Author: logan
     */
    
    #include <stdio.h>
    #include "treeattempt1.h"
    
    int numbers[3]={1,2,3};
    
    int main() {
    	// create a tree with three branches
    	struct element branches[3];
    	struct node tree;
    	tree.elements=&branches[0];
    
    	// before we populate tree we need to point each child nodes to root of the tree
    	struct node childs[3];
    	int m=0;
    	for (m=0; m<3; m++) {
    		tree.elements[m].child_node=&childs[m];
    	}
    
    	// fill tree with numbers 1, 2, 3
    	int i=0;
    	for (i=0; i<3; i++) {
    		tree.elements[i].child_node->value=numbers[i];
    	}
    
    	// try printing out values in the tree
    	int j=0;
    	for (j=0; j<3; j++) {
    		printf("%d", tree.elements[j].child_node->value);
    	}
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by gunitinug View Post
    Don't call me wacko though. I was trying to create a tree like structure.
    Calm down. He didn't call you a wacko. He called your data structure wacky and imho he is right.

    Code:
    /*
     * treeattempt1.h
     *
     *  Created on: 21/06/2012
     *      Author: logan
     */
    
    #ifndef TREEATTEMPT1_H_
    #define TREEATTEMPT1_H_
    
    
    #endif /* TREEATTEMPT1_H_ */
    
    struct node {
        int value;
        struct element* elements;
    };
    
    struct element {
        struct node* child_node;
    };
    If you want to avoid including your header more than once in a program, your method doesn't work. You have to put the whole code inside the #ifndef and #endif block.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-23-2012, 04:26 PM
  2. entering 0s and negative values into while
    By cool_guy in forum C++ Programming
    Replies: 5
    Last Post: 04-14-2010, 06:34 PM
  3. Help with nested struct pointers
    By vampireiam in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 10:59 AM
  4. nested struct , fread ,fwrite
    By magicz69 in forum C Programming
    Replies: 4
    Last Post: 08-01-2004, 09:47 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

Tags for this Thread


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