Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9113567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:59:44+00:00 2026-06-17T03:59:44+00:00

Basically what I’m trying to achieve here is having global variable with array of

  • 0

Basically what I’m trying to achieve here is having global variable with array of pointers to struct which size isn’t known at compile time — in my example below it’s my_struct **tab. In the final version I want to call a JNI method which will initialize my array of pointers and I want to keep them for some other methods usage.

Unfortunately I’m not a C programmer and I really struggle with this problem. Below I show what I tried to do; obviously, it’s not working. Any constructive feedback would be really helpful.

(Sorry for missunderstanding with includes it’s supposed to be a C code )

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

typedef struct {
    int tag;
} my_struct;

my_struct **tab;

void * get_pointer_to_struct() {

    my_struct * s;
    /* allocate memory */
    if ((s = (my_struct *) malloc(sizeof (my_struct))) == NULL) {
        return NULL;
    }
    return s;
}

void free_structures(int j) {
    for (int a; a < j; a++) {
        my_struct *s;
        s = (my_struct *) tab[a];

        /* free memory */
        free(s);
        tab[a] = NULL;
    }
}

void init_pointers_array(int j) {
    my_struct * temp_arr[j];
    for (int i = 0; i < j; i++) {
        temp_arr[i] = (my_struct *) get_pointer_to_struct();
        temp_arr[i]->tag = i;
    }
    tab = temp_arr;
}

int main() {
    //initialization
    init_pointers_array(10);
    //usage
    for (int a = 0; a < 10; a++) {
        if (tab[a]) {
            my_struct * str_tmp = tab[a];
            printf("Integer that you have entered is %d\n", str_tmp->tag);
        }
    }
    //free mem
    free_structures(10);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:59:45+00:00Added an answer on June 17, 2026 at 3:59 am
    my_struct * temp_arr[j];
    

    then

    tab = temp_arr;
    

    is wrong. (Not only the placement of the * qualifier is horrible and there are superfluous casts that severely decrease code readibility, but) temp_array is a local auto array, so it will be deallocated when the function returns. Doing anything with its address afterwards results in undefined behavior. You may want to malloc()ate some memory for the struct instead (the casts are only there in order the code to be usable in C++. In C, it’s strongly discouraged to make redundant typecasts):

    my_struct **tab;
    
    tab = (my_struct **)malloc(sizeof(tab[0]) * number_of_structs);
    
    int i;
    for (i = 0; i < number_of_structs; i++) {
        tab[i] = (my_struct *)malloc(sizeof(tab[0][0]));
    }
    

    And for freeing it:

    int i;
    for (i = 0; i < number_of_structs; i++) {
        free(tab[i]);
    }
    
    free(tab);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically what I'm trying to do is generate an alphanumeric string, which is passed
Basically what i am trying to do here is to read from the table
Basically, I'm just looking for pointers here. Ideally, I'd just supply a GPS coordinate
Basically, what I'm trying to create is a page of div tags, each has
Basically, I have slider which I would like to be triggered from menu too.
Basically I am trying to do this: <Trigger Property=Item Value={c:CollectionView.NewItemPlaceHolder}> But the syntax is
Basically I am wondering about having this behavior in an app where the newer
Basically, I was hoping to sort of keep my files sorted instead of having
Basically I have a TcxGrid which will be listing various files names and I'd
Basically I need to do String.IndexOf() and I need to get array of indexes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.