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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:24:24+00:00 2026-05-25T00:24:24+00:00

i am trying to create a 2d array using double pointer… my code is…

  • 0

i am trying to create a 2d array using double pointer…
my code is…


int **p1;
p1=(int **) malloc(2*sizeof(int *));
for(int i=0;i<2;i++)
{
    p1[i]=(int *) malloc(3*sizeof(int));
    for(int j=0;j<3;j++)
    {
       scanf("%d",(p1+i)+j);
    }

}
for(int i=0;i<2;i++)
{

    for(int j=0;j<3;j++)
    {
       printf("%d\n",*(*(p1+i)+j));
    }

}

as i have declared a double pointer (**p1) for it and i am able to put data at all those places using my scan scanf("%d",(p1+i)+j); statement. And to dereference i can use print statement as i have done printf("%d\n",*(*(p1+i)+j));

but why it is breaking during print statement but accepting my scan statement.

but why this is giving me correct response…

int mybox[][4]={{1,2,3,4},{5,6,7,8}};//we have to provide subscript;
for(int i=0;i<2;i++){
     for(int j=0;j<4;j++){
        printf("%d",mybox[i][j]);//will print all elements
        }
}
        printf("%d",*(*(mybox)+1));//give me 2
        printf("%d",*(*(mybox+1)+1));//give me 6
        printf("%d",*(*(mybox+1)+3));//give me 8
  • 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-05-25T00:24:25+00:00Added an answer on May 25, 2026 at 12:24 am

    Your scanf portion is implemented incorrectly. It destroys your data. Ironically, the problem does not reveal itself during scanf phase, but instead causes a crash at printf stage.

    The correct scanf code might look as follows

    for(int i=0;i<2;i++)
    {
        p1[i]=(int *) malloc(3*sizeof(int));
        for(int j=0;j<3;j++)
        {
           scanf("%d",*(p1+i)+j);
        }
    }
    

    Note the extra *. What you really need here is

    scanf("%d", &(*(*(p1 + i)) + j));
    

    You can write it this way if you want. But you can notice that the outer & “annihilates” with the next nested *, so the above is equivalent to

    scanf("%d", *(p1 + i) + j);
    

    However, my advice to you is to stop using the barely-readable *-and-+ combination for array element access and start using the [] instead. This is how your code should have looked from the very beginning

    ...
    scanf("%d", &p1[i][j]);
    ...
    printf("%d\n", p1[i][j]);
    

    On top of that, since this is taggged C, avoid using sizeof with types as much as possible and stop casting the result of malloc. A better variant would be

    int **p1;
    
    p1 = malloc(2 * sizeof *p1);
    for(int i = 0; i<2; i++)
    {
      p1[i] = malloc(3 * sizeof *p1[i]);
      ...   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an array of strings in C using malloc .
I am trying to create a multi dimensional array using this syntax: $x[1] =
I'm trying to create a Quicksort base class using VB.NET, taking it an array
I am trying to create a map using two arrays. Here is the code:
I am trying to create a 2d array using nested containers. I want to
I'm trying to create a custom JSP tag that would take an array object
I was trying to create a pseudo super struct to print array of structs.
I'm trying to create a function which takes an array as an argument, adds
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
I have a function which grows an array when trying to add an element

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.