Hello All,
I'm new here, and glad to be part of this wonderful and helpful site.

I'm trying to allocate a new array (lets say of size 10) inside a struct.
I want to do it this way:

#include <stdlib.h>
#include <stdio.h>

typedef struct Circle {
    int *x;
    int y;
    int r;
} Circle;

typedef struct Circle* CircleP;
typedef struct Circle Circle;

//initialize array of size 10.
CircleP allocateMem() {
    CircleP p=(CircleP) malloc(sizeof(Circle));
    p->x=(int*)malloc(sizeof(int) * 10);
    return p;
}

int main() {
    Circle circle;
    CircleP circleP=&circle;
    circleP=allocateMem();
    int i;
    for(i=0;i<10;i++)
        circleP->x[i]=i;
    int j;
    for(j=0;j<10;j++)
        printf("%d\n", circleP->x[i]);
    return 0;
}

The program do compile, but when I print the array I get 0 as its values.
What I'm doing wrong?

Thanks

Dani AI

Generated

Good catch: the values print as 0 because the second loop uses j as the counter but prints x[i]. By the time that loop runs, i is 10, so you read past the end of the array (undefined behavior). Print x[j] instead. Also, no need to create a stack Circle and then overwrite its pointer; just allocate and use it. Finally, always check allocations and free what you allocate.

Here is a small, safe pattern you can drop in:

#include <stdio.h>
#include <stdlib.h>

struct Circle { int *x; int y; int r; };

struct Circle *make_circle(size_t n) {
    struct Circle *p = malloc(sizeof *p);
    if (!p) return NULL;
    p->x = malloc(n * sizeof *p->x);
    if (!p->x) { free(p); return NULL; }
    p->y = 0; p->r = 0;
    return p;
}

int main(void) {
    size_t n = 10;
    struct Circle *c = make_circle(n);
    if (!c) return 1;

    for (size_t i = 0; i < n; ++i) c->x[i] = (int)i;
    for (size_t j = 0; j < n; ++j) printf("%d\n", c->x[j]);

    free(c->x);
    free(c);
    return 0;
}

Notes and options:

  • In C, do not cast malloc; include <stdlib.h> and use malloc(sizeof *ptr) to avoid mismatches.
  • If the array is always size 10, prefer embedding it: struct Circle { int x[10]; int y; int r; }; No malloc needed.
  • If you want variable length without an extra pointer, use a flexible array member (C99+): struct Circle { int y, r; int x[]; }; and allocate with malloc(sizeof *c + n * sizeof c->x[0]); (the flexible member must be last).
  • Compile with -Wall -Wextra to catch mistakes like the i/j mix-up that pointed out, and to warn on missing checks.

Recommended Answers

All 4 Replies

Your printing loop counter is j, but you print x.

wow... I can't believe I did that..
Is everything beside that ok? That's how I should allocate memory to an array inside a struct?

Besides checking a return value, allocation is OK. Lines 22-24 look strange though. A simple

CircleP circleP = allocateMem();

is enough.

Thanks nezachem.. I'm new in programming and specially in c.
This Forum is really helpful, many topics helped me a lot for now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.