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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:27:56+00:00 2026-05-15T13:27:56+00:00

This is not so much a question on, How do I pass it into

  • 0

This is not so much a question on, “How do I pass it into the function?” but rather, “Is this acceptable?”

void func( int **ptr );

int main( int argc, char* argv[] )
{
    int arr[][3] = {{1, 2,}, {3, 4}, {5, 6}};
    int *pArr = *arr;

    (&pArr[0])[1] = 3;

    func(&pArr);

    cin.get();
    return 0;
}

void func( int **ptr )
{
    cout << "In func()" << endl;
    ptr[0][1] = 5;
}

This works as far as I can tell. It doesn’t really feel safe to me, but I like it more than passing a 2D array into a function. Instead, a pointer that does the work for me.

Would this be very confusing for people who had to read my code? Should I use other methods instead? Is it a bad idea to work with pointers to arrays?

Also, question a little bit off-topic. Why can I write:

int arr[] = { 1, 2, 3, 4, 5 };
int *pArr = *arr;

but why can’t I write

int arr[][3] = {{1, 2,}, {3, 4}, {5, 6}};
int *pArr = **arr;

or even use a **pArr?

  • 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-15T13:27:57+00:00Added an answer on May 15, 2026 at 1:27 pm

    Let’s dissect this carefully, since array-to-pointer conversions are sometimes confusing.

    int arr[][3] = {{1, 2,}, {3, 4}, {5, 6}};
    

    arr is now an array of 3 arrays of 3 ints.

    int *pArr = *arr;
    

    *arr uses the array arr in expression, so it decays to a pointer to the first element of arr — that is pointer to array of 3 ints (the array containing {1,2,0}). Dereferencing that pointer (with *) gives you the array of 3 ints. Now you’re using that array in an expression and it decays to a pointer to int, which is assigned to pArr.

    (&pArr[0])[1] = 3;
    

    pArr[0] gives the integer at which pArr is pointing (the number 1). &pArr[0] makes a pointer at that integer (which is actually equal to pArr). Indexing that pointer with [1] gives a reference to the next integer after the number 1, which is the number 2. To that reference you’re assigning 3. Here’s the catch: pointer to an element of an array can only be used to access other elements of the same array. Your pointer points at an element of the array {1, 2, 0}, which you’ve changed to {1, 3, 0}, and that’s fine, but

    func(&pArr);
    

    Now you’re creating a pointer to pointer to int (since pArr was a pointer to int), and passing that to your function.

    ptr[0][1] = 5;
    

    And now you’ve taken ptr[0], which evaluates to the pointed-to object, which is your original pointer pArr. This line is equivalent to pArr[1] = 5;, and that is still valid (changing your {1,2,0} array to {1,5,0}). However, ptr[1][... would be invalid, because ptr is not pointing at an element of an array of any kind. It’s pointing at a standalone pointer. Incrementing ptr will make it point at uninitialized memory and dereferencing that will be undefined behavior.

    And for the additional questions:

    You should not be able to write this:

     int arr[] = { 1, 2, 3, 4, 5 };
     int *pArr = *arr;
    

    The array arr decays to a pointer-to-int (pointing at the number 1), dereferencing that gives the integer, and an integer cannot be assigned to the pointer pArr. gcc says error: invalid conversion from ‘int’ to ‘int‘*.
    Likewise, you cannot write this:

    int arr[][3] = {{1, 2,}, {3, 4}, {5, 6}};
    int *pArr = **arr;
    

    for the same reason: *arr is the array of 3 ints {1, 2, 0}, **arr is the integer 1, and an integer cannot be assigned to a pointer.

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

Sidebar

Ask A Question

Stats

  • Questions 442k
  • Answers 442k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "FirstName".replace(/([a-z])([A-Z])/g, '$1 $2') That results in First Name Without a… May 15, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer You might be interestesd in the Mobile Device Browser File.… May 15, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer Using custom_url_rewrite_inbound() for this would be somewhat similar to using… May 15, 2026 at 5:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.