Thread: Array with n initialized instance

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    11

    Array with n initialized instance

    Hello everyone !

    I have one more problem with a programming exercise in c.

    I have this struct
    Code:
    struct FluxCapacitor
        {
            unsigned char* c_string;
            unsigned int value;
        };
    I initialized the struct and now I want to create an Array which creates n completly initialized instance of my struct.

    I started like this

    Code:
    struct FluxCapacitor** createFluxCapacitorArray(unsigned int n){
    
        struct Flux **array = malloc(n * sizeof *array); // Flux is the new Struct name
    I created a new function and I allocated the memory for the array.

    How would my next step look like?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If you're just trying to create an array of structs then you don't need a double pointer. Just malloc size elements and then fill them in with a loop, just like with a normal array. Remember to free the array when you're done with it.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    11
    Well the function name was given in the task position with the double pointer.

    I will put my complete code in so its maybe easier to understand what I want to do

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    struct FluxCapacitor
        {
            unsigned char* c_string;
            unsigned int value;
        };
    
    char* randomString() //Function for random character
    {
        unsigned char* character;
        character = malloc(sizeof(unsigned char));
        int i;
        for (i=0; i<3; i++)
        {
            character[i] = (unsigned char) (65+(rand()%26));
        }
        character[3] ='\0';
        return character;
    }
    
    
    void bubbleSortFluxCapacitorArray(struct FluxCapacitor** array, unsigned int n){ //Not in use right now
    
    }
    
    void printFluxCapacitorArray(struct FluxCapacitor** array, unsigned int n){ // Not in use right now
    
    
    
    }
    
    struct FluxCapacitor* createFluxCapacitor(){ // Instance of my struct
        struct FluxCapacitor *Flux = malloc( sizeof(*Flux) );
    Flux->value = rand();
    Flux->c_string = randomString();
    };
    
    
    struct FluxCapacitor** createFluxCapacitorArray(unsigned int n){ //Function for the Array I want to initialize with n instance of the struct
    
        struct Flux **array = malloc(n * sizeof *array);
    
        return array;
    
    
    };
    
    int main()
    {
    
    
    }
    Last edited by Jimmy589; 04-05-2017 at 02:09 PM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't know what a "task position" is, but if you have your heart set on a double pointer then you could malloc the structs separately. You'll have to free them all separately, too, and then free the pointer array.
    Code:
    typedef struct FC {
      int x;
    } FC;
    
    void *xmalloc(size_t size) {
      void *p = malloc(size);
      if (!p) {
        perror("xmalloc");
        exit(EXIT_FAILURE);
      }
      return p;
    }
    
    FC **FC_new(int size) {
      FC **fc = xmalloc(size * sizeof *fc);
      for (int i = 0; i < size; i++) {
        fc[i] = xmalloc(sizeof **fc);
        fc[i]->x = 0;
      }
      return fc;
    }

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    11
    I edit my code in and in line 35 I create my instance of the struct. I filled the attributes with random values and now I want to create an Array with n completly initialized instances of my Struct.

    For example n would be 10 so I want to fill my Array with 10 instances of the struct. So if I print the Array I would get 10 random numbers from value and 10 random characters.
    Last edited by Jimmy589; 04-05-2017 at 02:21 PM.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Sounds like a plan. Make it so.

  7. #7
    Registered User
    Join Date
    Apr 2017
    Posts
    11
    Well thats my question how do I do this?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > character = malloc(sizeof(unsigned char));
    Look at the loop that follows. How many characters do you need to allocate here?

    > struct Flux **array = malloc(n * sizeof *array);
    I guess you need next is something like
    array[i] = createFluxCapacitor();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2017
    Posts
    11
    The function looks like this now

    The string is 3 characters long so I need to allocate 3*n characters

    Code:
    struct FluxCapacitor** createFluxCapacitorArray(unsigned int n){ //Function for the Array I want to initialize with n instance of the struct
    
        struct Flux **array = malloc(n * sizeof *array);
    
        for (int i = 0 ; i < n ; ++i)
         {
             array[i] = createFluxCapacitor();
         }
    
    };

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The string is 3 characters long so I need to allocate 3*n characters
    Did you count the \0 at the end?

    What does your createFluxCapacitorArray() function return?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2017
    Posts
    11
    Yes I count the \0 at the end

    It returns the Array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-09-2014, 05:06 PM
  2. qsort with array initialized using malloc?
    By slug in forum C Programming
    Replies: 3
    Last Post: 03-02-2013, 02:06 AM
  3. the variable is being used without being initialized c
    By bob_999 in forum C Programming
    Replies: 7
    Last Post: 01-09-2011, 04:38 AM
  4. Static array of initialized members?
    By Blackroot in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2009, 09:36 PM
  5. Delete instance of a class array
    By wbeasl in forum C++ Programming
    Replies: 6
    Last Post: 10-22-2007, 07:56 AM

Tags for this Thread