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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:57:53+00:00 2026-05-16T16:57:53+00:00

Okay, I have a multi-dimensional array which is statically-allocated. I’d very much like to

  • 0

Okay, I have a multi-dimensional array which is statically-allocated. I’d very much like to get a pointer to a portion of it and use that pointer to access the rest of it. Basically, I’d like to be able to do something like this:

#include <stdio.h>
#include <string.h>

#define DIM1 4
#define DIM2 4
#define DIM3 8
#define DIM4 64

static char theArray[DIM1][DIM2][DIM3][DIM4] = {0};

int main()
{
    strcpy(theArray[0][0][0], "hello world");

    char** ptr = theArray[0][0];

    printf("%s\n", ptr[0]);

    return 0;
}s

This code results in this error using gcc:

t.c: In function ‘main’:
t.c:17: warning: initialization from incompatible pointer type

char** is apparently not the correct type to use here. I assume it’s because statically-allocated arrays are created as a single block in memory while dynamically allocated ones are separated in memory, with each dimension having a pointer to the next one.

However, as you likely noticed, the number of dimensions here is painfully large, and I’m obviously going to need to use actual variables to index the array rather than the nice, slim character 0, so it’s going to get painfully long to index the array in actual code. I’d very much like to have a pointer into the array to use so that accessing the array is much less painful. But I can’t figure out the correct syntax – if there even is any. So, any help would be appreciated. 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-05-16T16:57:54+00:00Added an answer on May 16, 2026 at 4:57 pm

    theArray[0][0] gets rid of the first two dimensions, so you have something of type char [DIM3][DIM4]. The first dimension will drop out when the array decays to a pointer, so the declaration you want is:

    char (*ptr)[DIM4] = theArray[0][0];
    

    For what it’s worth, gcc also displays a warning for your array declaration: “warning: missing braces around initializer”. Static variables and global variables will automatically be initialized to 0, so you can fix the warning by getting rid of the initializer:

    static char theArray[DIM1][DIM2][DIM3][DIM4];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array like so: var map = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]; I then have
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay I have a series of objects based on a base class which are
Okay I have a project in CMake structured like this: CMakeLists.txt /libfoo/CMakeLists.txt /frontend/qt/CMakeLists.txt libfoo
Okay, basically, I have formed a mySQL query which returns a data set all
Okay I have the following problem. I want to get the current dateTime and
Okay so with Customer Attributes I have a multi-option selection that I have added
I have implemented a multi-threaded program which involves spawning a thread for each user,and
Okay I have this RewriteRule which is supposed to redirect any request for the

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.