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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:14:05+00:00 2026-05-24T05:14:05+00:00

I have a function which tries to loop through a 2D array in a

  • 0

I have a function which tries to loop through a 2D array in a struct:

typedef struct node
{
    int grid[3][3];
} Node;


void someFunction(Node *node) {
     int grid[3][3] = node->grid;
     //loop through
}

When I try to compile this however I get a

mp.c:42: error: invalid initializer

  • 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-24T05:14:06+00:00Added an answer on May 24, 2026 at 5:14 am

    You cannot assign arrays in C. It is simply not allowed. When you wrote:

    int grid[3][3] = node->grid;
    

    you were attempting to initialize the local array, grid, from the passed in node. If that were permitted (which it isn’t), then you’d not need a loop afterwards.

    You can assign structures, though, even if they contain arrays, so if the local structure were a Node, you could have written:

    Node local = *node;
    

    You would not need to loop through the arrays afterwards to initialize local.

    You can loop through the arrays, doing the copy one element at a time:

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            grid[i][j] = node->grid[i][j];
    

    You can also use memmove() or memcpy():

    int grid[3][3];
    
    assert(sizeof(grid) == sizeof(node->grid));
    memcpy(grid, node->grid, sizeof(grid));
    

    At one time, another answer suggested:

    Change the line:

    int grid[3][3] = node->grid;
    

    to:

    int **grid = node->grid;
    

    I noted that this will not work – and was legitimately challenged to explain why. That requires space and formatting.

    First of all, the compiler notes:

    warning: initialization from incompatible pointer type
    

    That is saying ‘you are playing with fire’.

    Let’s suppose we ignore that warning. The local grid now points at the top-left corner of the array (if you see arrays growing down and across from left-to-right). The value stored there is a plain number, not an initialized pointer, but when the compiler evaluates grid[0], it is forced to assume that will produce a pointer. If node->grid[0][0] contains a zero, you will probably get a segmentation fault and core dump for dereferencing a null pointer (on the assumption that pointers and int are the same size, which is generally true on 32-bit systems), or some other undefined behaviour. If node->grid[0][0] contains another value, then the behaviour is still undefined, but not quite as predictable.

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

Sidebar

Related Questions

I have postgresql (in perlu) function getTravelTime(integer, timestamp), which tries to select data for
i have a function which retrieves values from a webservice , then loops through
I have a merge function which takes time O(log n) to combine two trees
I have a function which parses one string into two strings. In C# I
I have a function which searches an STL container then returns the iterator when
I have an function which decodes the encoded base64 data in binary data but
I have a function which gets a key from the user and generates a
I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3='a
I have a function which returns a one-sided intersection of values between two input
I have foreach function which calls specified function on every element which it contains.

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.