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

  • SEARCH
  • Home
  • 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 8241243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:46:19+00:00 2026-06-07T20:46:19+00:00

I’m doing something silly here and I can’t put my finger on exactly what:

  • 0

I’m doing something silly here and I can’t put my finger on exactly what:

 void init_data(double **data, int dim_x, int dim_y) {

    int i,j,k;

    data = (double **) malloc(sizeof(double) * dim_x);
    for (k = 0; k < dim_y; k++) {
        data[k] = (double *) malloc(sizeof(double) * dim_y);
    }

    for (i = 0; i < dim_x; i++) {
        for (j = 0; j < dim_y; j++) {
            data[i][j] = ((double)rand()/(double)RAND_MAX);
        }
    }
}

And in main() I do the following:

double **dataA;
int dim = 10; 
init_data(&dataA, dim, dim);

But then right after that when I try printing the data the program crashes:

int i,j;
    for(i=0;i<dim;i++)
        for(j=0;j<dim;j++)
            printf("%d\n", dataA[i][j]);

What am I missing?

Thanks

  • 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-07T20:46:23+00:00Added an answer on June 7, 2026 at 8:46 pm

    You are making a few mistakes in your pointers. You are passing the &dataA to init_data, so the argument type should be ***double, instead of **double. Also your first malloc is initializing an array of pointers, not an array of doubles, so it should be sizeof(double *) * dim_x. The code below should work.

    void init_data(double ***data_ptr, int dim_x, int dim_y) {
      int i,j,k;
      double **data;
      data = (double **) malloc(sizeof(double *) * dim_x);
      for (k = 0; k < dim_x; k++) {
          data[k] = (double *) malloc(sizeof(double) * dim_y);
      }
    
      for (i = 0; i < dim_x; i++) {
          for (j = 0; j < dim_y; j++) {
              data[i][j] = ((double)rand()/(double)RAND_MAX);
          }
      }
      *data_ptr = data;
    }
    
    void main() {
      double **dataA;
      int dim = 10;
      init_data(&dataA, dim, dim);
      int i,j;
          for(i=0;i<dim;i++)
              for(j=0;j<dim;j++)
                  printf("%f\n", dataA[i][j]);
    }
    

    Your first loop should also have the condition k < dim_x instead of k < dim_y. It doesn’t matter in the current case since both dimensions are the same, but would cause issues if they weren’t. Finally, you should use %f instead of %d in your printf, as doubles are stored in a different format than integers, and you are likely to get gibberish instead than what you want.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I am trying to loop through a bunch of documents I have to put

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.