3

I have created below C struct.

typedef struct KnightsMartSale {
char firstName[21];
char lastName[21];
int numItemsOnList;
int *itemsPurchased; // array of item numbers
struct KnightsMartSale *next;
} KMSale;

Here is it possible to assign int array to the int *itemsPurchased pointer? If it is possible how print the values?

6
  • 2
    The thing you want is called flexible array member. Commented Feb 8, 2019 at 10:22
  • And that needs to be the last member in the structure. Commented Feb 8, 2019 at 10:25
  • Or it could just be a pointer to a malloced array. Commented Feb 8, 2019 at 10:26
  • Anyone show me an example. I'm new to C language. Commented Feb 8, 2019 at 10:29
  • 1
    Does he want flexible arrays in the middle of a linked list? Or just asking for itemsPurchased = malloc(sizeof(int) * n)? Commented Feb 8, 2019 at 10:42

3 Answers 3

4

I'd allocate memory according to the size of the array you want to copy into itemsPurchased and also "remember" the number of possible items in numItemsOnList.

So suppose you have a given array of ints, let's say myArray, then the code for copying and printing could look as follows:

typedef struct KnightsMartSale {
    char firstName[21];
    char lastName[21];
    int numItemsOnList;
    int *itemsPurchased; // array of item numbers
    struct KnightsMartSale *next;
} KMSale;

int main() {

    KMSale kmsale;

    int myArray[] = { 20,30,40,50 };

    kmsale.numItemsOnList = sizeof(myArray)/sizeof(myArray[0]);
    kmsale.itemsPurchased = calloc(kmsale.numItemsOnList,sizeof(int));
    memcpy(kmsale.itemsPurchased,myArray,kmsale.numItemsOnList*sizeof(int));

    for (int i=0; i<kmsale.numItemsOnList; i++) {
        printf("item #%d: %d\n",i,kmsale.itemsPurchased[i]);
    }


    // kmsale not needed any more, free memory:
    free(kmsale.itemsPurchased);
}

Output:

item #0: 20
item #1: 30
item #2: 40
item #3: 50
Sign up to request clarification or add additional context in comments.

1 Comment

Any particular reason why you use calloc followed by memcpy? malloc would achieve the same thing, but faster.
0

Just some rapid prototype coding... Perhaps it might direct you into the right way...

KMSale foo; // sample struct on stack, not initialized!
int my_buffer[12]; // not initialized stack buffer!

/* Assign pointer */
foo.itemsPurchased = my_buffer; // point to my_buffer

/* Print the first element via the struct... */
printf("%02x", foo.itemsPurchased[0]);

1 Comment

Why on earth was my answer down voted? Because I did not solve the homework entirely, otherwise tell me whats wrong in the code!? The "right" solution proposes a dynamic memory allocation on a super small buffer, the for loop counter variable is out of scope, etc.
0

Here is it possible to assign int array to the int *itemsPurchased pointer? If it is possible how print the values?

Yes we can assign an array to a pointer as array is a constant pointer and reverse is invalid.

But this assignment should be used very carefully as array will be a stack variable and scope of the variable should be taken care before accessing this structure pointer

Also this method can be preferred over the dynamic memory allocation where memory fragmentation is a concern by malloc and free and we can avoid the dynamic allocation overhead.

Following is the code for this and output of print value in the array:

#include <stdio.h>

typedef struct KnightsMartSale {
    char firstName[21];
    char lastName[21];
    int numItemsOnList;
    int *itemsPurchased; // array of item numbers
    struct KnightsMartSale *next;
} KMSale;

int main() {

    KMSale sale;
    int iPos = 0;

    int Array[] = {1, 2, 3, 4, 5};

    sale.numItemsOnList = sizeof(Array) / sizeof(Array[0]);
    sale.itemsPurchased = Array;

    for (iPos=0; iPos < sale.numItemsOnList; iPos++) {
        printf("sale %d: %d\n", iPos, sale.itemsPurchased[iPos]);
    }

    return 0;
}

output:

sale 0: 1
sale 1: 2
sale 2: 3
sale 3: 4
sale 4: 5

Comments

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.