Thread: problem with structure

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    16

    problem with structure

    Hey! I just started with structs so it probably will be a stupid fail, but I dont give with the clue! Thats part of my code:
    Code:
    struct Array{
           int v[];
           int n;
    };
    
    
    Array ArrayCreaAleatorio(int linf,int lsup){
    
    
    int linf2;
    linf2 = linf; 
          struct Array array1;
          array1.n = lsup-linf+1;
          for (int i = 0; i=n; i++){
          array1.v[i] = linf2++;
          linf2 = linf2++;
          }
    
    
     for (int j = 0; j = (n-2); j++){
         v.array1[j] = (rand()% lsup)+linf;
         }  
    return array1;  
    }
    And I get these errors:
    `n' undeclared (first use this function)
    `v' undeclared (first use this function)
    Last edited by zetaXX; 05-27-2013 at 02:31 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    `n' undeclared (first use this function) -> should be array1.n
    `v' undeclared (first use this function) -> should be array1.v

    You have to allocate some memory for array1.v, as is array1.v points to a random location

    Kurt

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by ZuK View Post
    `n' undeclared (first use this function) -> should be array1.n
    `v' undeclared (first use this function) -> should be array1.v

    You have to allocate some memory for array1.v, as is array1.v points to a random location

    Kurt
    And how do I do it?

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by zetaXX View Post
    And how do I do it?
    example program that uses typedefs:

    Code:
    #include <malloc.h>
    
    typedef struct Array{
        int n;
        int v[];
    }Array_t, *PArray_t;
    
    int main(int argc, char *argv[])
    {
    int i;
    int size = 10;
    PArray_t pArray;
    
        pArray = malloc(sizeof(Array_t) + size*sizeof(int));
    
        pArray->n = size;
        for(i = 0; i < size; i++)
            pArray->v[i] = i;
    
        free(pArray);
    
        return(0);
    }
    For a more generic allocation based on the size of an element of v you could use:
    Code:
        pArray = malloc(sizeof(Array_t) + size*sizeof(((PArray_t)0)->v[0]));
    Last edited by rcgldr; 05-27-2013 at 04:20 PM.

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by rcgldr View Post
    example program that uses typedefs:

    Code:
    #include <malloc.h>
    
    typedef struct Array{
        int n;
        int v[];
    }Array_t, *PArray_t;
    
    int main(int argc, char *argv[])
    {
    int i;
    int size = 10;
    PArray_t pArray;
    
        pArray = malloc(sizeof(Array_t) + size*sizeof(int));
    
        pArray->n = size;
        for(i = 0; i < size; i++)
            pArray->v[i] = i;
    
        free(pArray);
    
        return(0);
    }
    For a more generic allocation based on the size of an element of v you could use:
    Code:
        pArray = malloc(sizeof(Array_t) + size*sizeof(((PArray_t)0)->v[0]));
    So I have to use malloc on my struct?
    I mean

    v = (int*)malloc ( n*sizeof(int) );It is right?

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by zetaXX View Post
    So I have to use malloc on my struct?

    v = (int*)malloc ( n*sizeof(int) );It is right?
    You need to use malloc for the entire struct if you use the example I posted before.

    If you only want to allocate v, then your struct needs to be changed to:

    Code:
    struct Array{
        int n;
        int *v;
    };

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Assuming your compiler supports variable size arrays at the end of a struct, you need to change the order of the struct to:

    Code:
    struct Array{
        int n;
        int v[];
    };
    You will also have to allocate memory (malloc()) for this structure, since the size of the struct is variable.
    Last edited by rcgldr; 05-27-2013 at 04:06 PM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You know the old ditty about the hole in the bucket?

    The cause of problems in your latest post is the exact same cause as the problems in your original post.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    for ( j = 0; j = (n-2); j++){
         v.array1[j] = (rand()% lsup)+linf;
         }
    you have to change v and n here too.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Structure inside a structure
    By purush12 in forum C Programming
    Replies: 11
    Last Post: 05-18-2011, 06:16 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  4. Replies: 4
    Last Post: 11-22-2006, 12:20 PM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM