Thread: problem with structure

  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
    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.

  4. #4
    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?

  5. #5
    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.

  6. #6
    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?

  7. #7
    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;
    };

  8. #8
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by rcgldr View Post
    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;
    };
    but I need to V to be an array not a pointer lol!
    I cant uderstand you I think xD

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by zetaXX View Post
    int *v; ... but I need to V to be an array not a pointer.
    You can use it as an array, even though it's a pointer, for example, v[i].

  10. #10
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by rcgldr View Post
    You can use it as an array, even though it's a pointer, for example, v[i].
    Ok ok, I did but I get the same error in the function

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by zetaXX View Post
    Ok ok, I did but I get the same error in the function
    Did you make the previous suggested changes?

    You need to make these changes:

    Code:
    struct Array ArrayCreaAleatorio(int linf,int lsup){
    int i;  /* you can't wait until the for loop  for(int i = ... , since this is C and not C++) */
    /* ... */
        array1.n = lsup-linf+1;
        array1.v = malloc(array1.n * sizeof(int));
    /* ... */
    After you make these changes, and if you still have problems, post all of your new code.

  12. #12
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by rcgldr View Post
    Did you make the previous suggested changes?

    You need to make these changes:

    Code:
    struct Array ArrayCreaAleatorio(int linf,int lsup){
    int i;  /* you can't wait until the for loop  for(int i = ... , since this is C and not C++) */
    /* ... */
        array1.n = lsup-linf+1;
        array1.v = malloc(array1.n * sizeof(int));
    /* ... */
    After you make these changes, and if you still have problems, post all of your new code.
    Alright, sorry about taking too long, changes are made, thats my code
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<time.h>
    #include <malloc.h>
    
    
    struct Array{
        int n;
        int *v;
    };
    
    
    struct Array ArrayCreaAleatorio(int linf,int lsup){
    int i;
    int j;
    int linf2;
    linf2 = linf; 
          struct Array array1;
          array1.n = lsup-linf+1;
          array1.v = malloc(array1.n * sizeof(int));
          for ( i = 0; i=array1.n; i++){
          array1.v[i] = linf2++;
          linf2 = linf2++;
          }
    
    
     for ( j = 0; j = (n-2); j++){
         v.array1[j] = (rand()% lsup)+linf;
         }  
         return array1;
    And the compiler gives me these errors:
    invalid conversion from void* to int*
    'n' undeclared (first use this function)
    'v' undeclared (first use this function)
    Last edited by zetaXX; 05-28-2013 at 05:16 AM.

  13. #13
    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.

  14. #14
    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

  15. #15
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by rcgldr View Post
    Did you make the previous suggested changes?

    You need to make these changes:

    Code:
    struct Array ArrayCreaAleatorio(int linf,int lsup){
    int i;  /* you can't wait until the for loop  for(int i = ... , since this is C and not C++) */
    /* ... */
        array1.n = lsup-linf+1;
        array1.v = malloc(array1.n * sizeof(int));
    /* ... */
    After you make these changes, and if you still have problems, post all of your new code.
    Alright I just needed (int *) between malloc and equal sign, thx!

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