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 8130597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:46:20+00:00 2026-06-06T08:46:20+00:00

i have a question for you! I have a C program in which i

  • 0

i have a question for you!

I have a C program in which i have a dynamic 2d array in my main function:

int **neighbours;

which i initialize based on args from command line.
The problem is that i want to clean up the code a bit. To do that i want to create a function in a header file that takes the array uninitialized as a reference argument and initialize it there.

This is how i call the function:

init(&nodeNum, &neighbours, argv[1], &nodes);

this is how i declare the function in the header file:

int init(int *nodeNum, int ***neighbours, char *arg, struct node **nodes)

and this is how try to allocate the memory:

neighbours=malloc(*nodeNum*sizeof(int *));
        for(i=0;i<*nodeNum;i++){
            neighbours[i] = malloc(*nodeNum*sizeof(int *));
        }

well obviously something doesn’t go well and it crashes! any hints on what i may be doing wrong?

thanks to the help of some nice people the malloc now works, but filling the nodes failes!

here is the whole code of the function:

int init(int *nodeNum, int ***neighbours, char *arg, struct node **nodes){
FILE *file;
char buffer[4];
int i,y,distance;

if(strcmp(arg,"-l")==0){
        file = fopen("C:\\Users\\trendkiller\\thesis\\matrix.txt","r");
        printf("parsing adjacency matrix from file\n\n");
        //file = fopen ("C:\\Users\\trendkiller\\thesis\\matrix.txt", "r" ) ;
        *nodeNum = atoi(fgets(buffer,4,file));
        *neighbours=malloc(*nodeNum*sizeof(int *));
        for(i=0;i<*nodeNum;i++){
            (*neighbours)[i] = malloc(*nodeNum*sizeof(int *));
        }
        printf("this prints\n");
        printf("number of nodes: %s", buffer);
        for(i=0;i<*nodeNum;i++){
            for(y=0;y<*nodeNum;y++){
                fgets(buffer,4,file);
                (*neighbours)[i][y]=atoi(buffer);
                            }
        }


    }
    else{
        *nodeNum = atoi(arg);

        *neighbours=malloc(*nodeNum*sizeof(int *));
        printf("this prints\n");
        for(i=0;i<*nodeNum;i++){
            (*neighbours)[i] = malloc(*nodeNum*sizeof(int *));
        }

        *nodes=malloc(*nodeNum*sizeof(struct node));

        for(i=0;i<30;i++){
            (*nodes)[i].x=rand()%100;
            (*nodes)[i].y=rand()%100;
        }
        printf("this prints\n");

        printf("creating new adjacency matrix\n\n");
        for(i=0; i<*nodeNum; i++){
            for(y=0; y<*nodeNum; y++){
                distance=sqrt((((*nodes)[y].x-(*nodes)[i].x)*((*nodes)[y].x-(*nodes)[i].x))+(((*nodes)[y].y-(*nodes)[i].y)*((*nodes)[y].y-(*nodes)[i].y)));
                if(i==y){
                    (*neighbours)[i][y]=-1;
                }
                else if(distance<=20){
                    (*neighbours)[i][y]=1;
                }
                else {
                    (*neighbours)[i][y]=0;
                }
            }

        }

        file = fopen("C:\\Users\\trendkiller\\thesis\\matrix.txt","a+");
        fprintf(file,"%d\n",*nodeNum);
        for(i=0;i<*nodeNum;i++){
            for(y=0;y<*nodeNum;y++){
                fprintf(file,"%d\n", (*neighbours)[i][y]);
            }
        }
    }

return 0;

}

Thanks in advance for your help!

  • 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-06T08:46:21+00:00Added an answer on June 6, 2026 at 8:46 am

    The problem is that neighbours is passed by value, so modifications to it inside the function have no effect. You need to pass it by pointer, and modify it indirectly:

    int init(int *nodeNum, int ***neighbours_ptr, char *arg, struct node *nodes) {
        *neighbours_ptr = malloc(*nodeNum*sizeof(int *));
        for(i=0;i<*nodeNum;i++){
            (*neighbours_ptr)[i] = malloc(*nodeNum*sizeof(int *));
        }
    }
    

    You also need to pass &neighbours instead of neighbours as the second argument of init.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick question here. I have a program in which I need to
I have a question on constant objects. In the following program: class const_check{ int
i have question how write program which calculates following procedures http://en.wikipedia.org/wiki/Tetration i have exponential
I have a program written in C++ which uses dlopen to load a dynamic
Question: Write a program which first defines a function evens(n). The function should take
Question: write a program which first defines functions minFromList(list) and maxFromList(list). Program should initialize
I have a function which is called multiple times during the program's execution. In
I have a program which takes various command line arguments. For the sake of
well, almost everything is in the question, I have a problem with program which
I have a C program which has multiple worker threads. There is a main

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.