Incrementing array index in a node.

This is a discussion on Incrementing array index in a node. within the C Programming forums, part of the General Programming Boards category; I have a struct like so: Code: struct array { char *array; AR next; } In another function, I have ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Incrementing array index in a node.

    I have a struct like so:

    Code:
    struct array {
      char *array;
      AR next;
    }
    In another function, I have declared a variable k.

    To increment the array index can I do:
    Code:
    current->array[k];
    ?

    I'm getting segmentation faults, so I'm thinking it's probably to do with that, so maybe I need to declare k in the struct and call it like this?

    Code:
    current->array[current->k++];
    ?

    But then when I do that it says I haven't declared next and k, which is strange =(
    =========================================
    Everytime you segfault, you murder some part of the world

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Also, can someone tell me what is wrong with this statement? It seems to segfault here:

    Code:
     while(current->array[current->k] != '\0') {
    The same happens when I just have k instead of current->k
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    current->array[k]
    doesn't do anything at all. It accesses array, says "oh that's a character" and ... that's it.

    I'm guessing you want
    Code:
    current->array[++k]
    which does the same "access array and not care about it" as before, but adds 1 to k before doing so.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is no field k in your array structure, so that code does not compile.

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I don't think I described in my first post too well.

    Code:
    struct string {
      char *array;
      AR next;
      int k;
    };
    Code:
    while(current != NULL) {
         while(current->array[current->k] != '\0') {
           string[i] = current->array[current->k];
           i++;
          current->k = current->k++;
         }
         current->k = 0;
         string[i] = '\n';
         i++;
         current = current->next;
      }
    =========================================
    Everytime you segfault, you murder some part of the world

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    current->k++ increments k, it doesn't need to be assigned back to itself.

    Guesses: (1) current->k doesn't get initialized before the start. (2) You inadvertently set current->array to a non-null-terminated sequence of characters, so you just keeep walking and walking and walking and walking.... (3) your struct string and "AR" are not equivalent, so following a next pointer fails. (4) i doesn't get reset to 0 in string -- although if you're writing a tokeniser that's probably the way it's supposed to be.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by tabstop View Post
    current->k++ increments k, it doesn't need to be assigned back to itself.

    Guesses: (1) current->k doesn't get initialized before the start. (2) You inadvertently set current->array to a non-null-terminated sequence of characters, so you just keeep walking and walking and walking and walking.... (3) your struct string and "AR" are not equivalent, so following a next pointer fails. (4) i doesn't get reset to 0 in string -- although if you're writing a tokeniser that's probably the way it's supposed to be.
    I figured it out, I had cleared the array earlier with another function, hence the segfault. Thanks for your help.
    Last edited by JFonseka; 04-15-2008 at 12:14 AM.
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Also, k does not need to be part of your struct for it to work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Elysia View Post
    Also, k does not need to be part of your struct for it to work.
    That's true, I had tried it within the function itself earlier but I was playing around trying to see what the segfault was.
    =========================================
    Everytime you segfault, you murder some part of the world

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Replies: 3
    Last Post: 12-06-2008, 06:54 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 01:05 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

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