0

I am new to C programming. Can any body tell me how can I do the coding for the term in bold?

  • create a structure called arrayData that contains an integer pointer called array and an integer variable called size.

  • create a function with the following header: arrayData* createArray(int size). Inside this function you will malloc space for a new arrayData structure. You will then need to create an array using the input variable as the number of elements. Finally you will need to set the variables in the malloc'ed arrayData pointer equal to the array and the array size. Finally return the pointer of malloc'ed arrayData structure.

I have tried something like:

#include<stdio.h>


struct arrayData
{
    int *array;
    int size;
}

struct arrayData* createArray(int size)
{
    struct arrayData *str = (struct arrayData*)malloc(sizeof(struct arrayData));
    int a = 10;
    int arr[a];
    for ( a = 0; a < 10; a++ )
    {
        str->arr[i] = a; 
    }
    return str;
}
1
  • Let's be honest, you didn't even try compiling that. arrayData has no member arr. Commented Aug 26, 2014 at 7:41

2 Answers 2

1

int arr[a]; is allocated locally inside the function and will be destroyed when the function returns. You should dynamically allocate str->array for proper allocation.

struct arrayData* createArray(int size)
{
    struct arrayData *str = malloc(sizeof(struct arrayData));
    int a = 10;
    str->array = malloc(size * sizeof(int));
    str->size = size;
    for ( a = 0; a < 10; a++ )
    {
        str->array[i] = a; 
    }
    return str;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just a few changes for allocating size and array

struct arrayData* createArray(int size)
{
  struct arrayData *str = (struct arrayData*)malloc(sizeof(struct arrayData));
  int a = 10;
  //int arr[size];       // array should be of size provided
  int *arr = (int*)malloc(size * sizeof(int));
  str->size = size;    // you should assign size to structure variable
  for ( a = 0; a < 10; a++ )
  {
      arr[i] = a; 
  }
  str->array = arr;  // you should point the array in structure to the 
                     // integer arr which you created
  return str;
}

3 Comments

int arr[size]; is allocated locally and will be destroyed once the function returns;
But if I want to Create a function with the following header: void setArray(arrayData *data). In this function you will set all values in the array of the input variable equal to a random number between 0 and 50. Create a function with the following header: void printArray(arrayData *data). This function will print out all values in the array of the input variable. How can I do this?
@RipalNaik - for this new question, make a separate thread/page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.