Thread: Character pointer problem

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    21

    Character pointer problem

    Hi all,

    I'll warn you all now this is my first attempt at programming in C so this is probably a very basic question. Basically I have an n-ary tree which contains a char pointer called value. I assign this a temporary value of say 'xyz'. I then parse this value to read each character and assign that character to a new node. The code to loop though the value is as follows:

    Code:
    for (j = 0; j < strlen(t->value); j++) {
    	add_child_node(t, &(t->value[j]));
    }
    The problem is if value is 'xyz' the first time round i get 'xyz' then the 2nd i get 'yz' and then finally 'z'. I can see why this happens but obviously it is not what i want. I just want 'x' then 'y' then 'z'. Can anyone give me some help on how I would do this?

    Thanks,
    James

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Have add_child_node take a character as the second parameter, and then just pass t->value[j]. Also, calculating strlen each iteration of the loop is very inefficient. You just need to calculate it once, after all.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    21
    Thanks for the quick reply! I have tried this and i get a compile error saying 'assignment makes pointer from integer without a cast'. Changing my add_child_node code to:

    Code:
    int add_child_node(struct node *t, char val) {
    	if (t->child_count >= MAX_CHILDREN) { return 0; }
    	
    	struct node *child;
    	child = tree_alloc();
    	if (child == NULL) { return 0; }
    	child->value = (char *)val;
    	child->parent = t;
    	child->child_count = 0;
    	
    	t->children[t->child_count] = child;
    	t->child_count++;
    	
    	return 1;
    }
    gives me a compiler warning 'cast to pointer from integer of different size'

    Thanks for pointing out the strlen problem I forgot that that gets checked every iteration!

    Cheers,
    James

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    You can't just cast a char to a char *, it doesn't work like that.

    How you accomplish this task depends on how you defined your struct.

    Code:
    If you allocated a fixed amount of space for the value, like this:
    
    struct node {
       ...
       char value[10];
       ...
    }
    
    Then you can store single character into the value by doing this:
    
    child->value[0] = val;
    child->value[1] = '\0';
    
    If instead the struct contains a char *, like this:
    
    struct node {
       ...
       char *value;
       ...
    }
    
    Then you would have to dynamically allocate some space before you stored the new value there:
    
    child->value = malloc(2 * sizeof(char));
    child->value[0] = val;
    child->value[1] = '\0';
    Notice how I made sure to add the trailing null-terminator onto the end of the value each time. This really is optional. But if you want to make sure that value contains a valid C string capable of being passed to functions like strlen(), then you must always make sure it is properly null terminated.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    21
    Excellent, thanks to both of you who replied. I've sorted the problem now using the approach suggested. It seems pretty obvious now you mention it!

    Cheers,
    James

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  3. pointer problem
    By peterx in forum C Programming
    Replies: 3
    Last Post: 11-11-2005, 02:02 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM