#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Code dosen't work


    Code:
    #include <stdio.h>
    
    int *list2array(void *list);
    
    int main() {
    
        // create a list with three nodes: 17 -> -2 -> 3]
        // with the first node at &data[2];
        int data[6];
        data[0] = -2;
        data[1] = (int)&data[4];
        data[2] = 17;
        data[3] = (int)&data[0];
        data[4] = 3;
        data[5] = 0;
    
        // call the function
        int *array;
        int  *array = (int *)list2array((void *)&data[2]);
    
        // print the result
        if (array[0] == 0) {
            printf("[]\n");
        }
        else {
            int i;
            printf("[%d", array[1]);
            for (i=2; i<=array[0]; i+=1) {
                printf(", %d", array[i]);
            }
            printf("]\n");
        }
        return 0;
    }
    Last edited by Winters; November 14th, 2013 at 09:38 AM. Reason: Added highlighting.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Could you be more specific?
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    1) Your program works perfectly. Behaves exactly as I expect.
    gcc -Wall c.c



    2) It fails because you didn't call the function "negligent" defined as
    Code:
    void negligent(void) {
      fputs("\nI won't bother to describe the problem.\n", stderr);
    }
    What is the program supposed to do? What is the input? What is the output for a test case or 3?


    3) Defining array twice hurts my compiler.
    Code:
      int *array;
      int *array = (int *)list2array((void *)&data[2]);
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo