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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:27:43+00:00 2026-06-16T04:27:43+00:00

Please consider the following code: #include <stdio.h> #include <stdlib.h> #define NUM_ARRAYS 4 #define NUM_ELEMENTS

  • 0

Please consider the following code:

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

#define NUM_ARRAYS     4
#define NUM_ELEMENTS   4
#define INVALID_VAL   -1

int main()
{
   int index            = INVALID_VAL;
   int array_index      = INVALID_VAL;
   int **ptr            = NULL;

   ptr = malloc(sizeof(int*)*NUM_ARRAYS);

   if (!ptr)
   {
      printf ("\nMemory Allocation Failure !\n\n");
      exit (EXIT_FAILURE);
   }

   for (index=0; index<NUM_ARRAYS; index++)
   {
      *(ptr+index) = malloc(sizeof(int)*NUM_ELEMENTS); 

      if (!*(ptr+index))
      {
         printf ("\nMemory Allocation Failure !\n");
         exit (EXIT_FAILURE);
      }
   }

   /* Fill Elements Into This 2-D Array */
   for (index=0; index<NUM_ARRAYS; index++)
   {
      for (array_index = 0; array_index<NUM_ELEMENTS; array_index++)
      {
         *(*(ptr+index)+array_index) = (array_index+1)*(index+1);
      }
   }

   /* Print Array Elements */
   for (index = 0; index<NUM_ARRAYS; index++)
   {
      printf ("\nArray %d Elements:\n", index);
      for (array_index = 0; array_index<NUM_ELEMENTS; array_index++)
      {
         printf (" %d ", *(*(ptr+index)+array_index));
      }
      printf ("\n\n");
   }

   return 0;
}

There is no problem with my code. It works fine.

Output:

Array 0 Elements:
 1  2  3  4 


Array 1 Elements:
 2  4  6  8 


Array 2 Elements:
 3  6  9  12 


Array 3 Elements:
 4  8  12  16 

I have a question about pointer arithmetic:

*(ptr+0) = Pointer to COMPLETE BLOCK (First Array)
*(ptr+1) = Pointer to COMPLETE BLOCK (Second Array).

But what is: (*ptr+1)?

GDB Output:

(gdb) p *(*ptr+1)
$1 = 2
(gdb) p *(*ptr+2)
$2 = 3
(gdb) p *(*ptr+3)
$3 = 4
(gdb) p *(*ptr+4)
$4 = 0

I am getting confused on this. Please provide me some explanation to resolve this doubt.

  • 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-16T04:27:44+00:00Added an answer on June 16, 2026 at 4:27 am

    *(ptr+i) is equals to ptr[i] and
    *(ptr+1) is ptr[1].

    You can think, a 2-D array as array of array.

    • ptr points to complete 2-D array, so ptr+1 points to next 2-D array.

    In figure below ptr is 2-D and number of columns are 3

    Original figure made by Mr. Kerrek SB, here , you should also check!

    +===============================+==============================+====
    |+---------+----------+--------+|+----------+---------+--------+|
    ||ptr[0,0] | ptr[0,1] | ptr[0,2]|||ptr[1,0] |ptr[1,1] | ptr[1,2]|| ...
    |+---------+----------+--------+++----------+---------+--------++ ...
    |            ptr[0]             |           ptr[1]              |
    +===============================+===============================+====
       ptr
    

    *(*ptr+1) = *( ptr[0] + 1 ) = ptr[0][1]

    Understand following:

    ptr points to complete 2-D.

    *ptr = *(ptr + 0) = ptr[0] that is first row.

    *ptr + 1 = ptr[1] means second row

    *(*ptr+1) = *(*(ptr + 0) + 1 ) = *(ptr[0] + 1) = ptr[0][1]

    Array 0 Elements:
    1  2  3  4 
    

    And GDB Output:

    (gdb) p *(*ptr+1)
    $1 = 2  
    

    that is correct 2 this can be read using ptr[0][1].

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

Sidebar

Related Questions

Please consider the following code: #include <stdio.h> int main() { static int counter=5; printf
Please consider the following Code: #include <stdio.h> int main() { int x; printf (\nEnter
Please consider the following piece of code #include <stdio.h> #define ROW_SIZE 2 #define COL_SIZE
please consider the following code : #include stdafx.h #include <stdio.h> #include <assert.h> #include <stdlib.h>
Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Please consider the following code. typedef struct{ int field_1; int field_2; int field_3; int
I'm confused about an aspect of polymorphism. Please consider the following code: #include <iostream>
Please consider the following code: vector<int> myVector; myVector.push_back(10); myVector.erase(myVector.end()); This code compiles and runs

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.