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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:19:19+00:00 2026-05-30T04:19:19+00:00

Let’s say that any C function has a pointer already declared, but not assigned

  • 0

Let’s say that any C function has a pointer already declared, but not assigned any value yet. We will int for our examples.

int *ptr;  

The goal of the function is not to assign ptr any dynamic memory on the heap, so no malloc call. Instead, we want to have it point to an array of fixed size n. I know I could accomplish this like so:

int arr[n];
ptr = arr;

However, the code could get very messy and hard to read if we need to do this many times in a function, ie, a struct of many pointer fields all need to point to an array of fixed length. Is there a better way to accomplish this in one line? I was thinking of something similar to below, but it looks too ambiguous and uncompilable:

int *ptr;
// Many other things happen in between...
ptr[n];

***EDIT***
Here, the below additional information may help guide some more answers (not saying that the current answers are not fine). In my use case, the pointers are declared in a struct and, in a function, I am assigning the pointers to an array. I want to know if there is a simpler way to accomplish this than in the below code (all pointers to point to fixed-length array):

struct foo {
    int* a;
    short* b;
    char* c;
    ...
};

void func(void) {
    struct foo f;
    int n = ...;
    int tempArr1[n];
    f.a = tempArr1;
    short tempArr2[n];
    f.b = tempArr2;
    char tempArr3[n];
    f.c = tempArr3;
    ...
}
  • 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-30T04:19:21+00:00Added an answer on May 30, 2026 at 4:19 am

    You cannot declare an array and assign it to an existing pointer in a single declaration. However, you can assign an array pointer to a newly declared pointer, like this:

    int arr[n], *ptr = arr;
    

    If you insist on staying within a single line, you could use an ugly macro, like this:

    #define DECL_ASSIGN_INT_ARRAY(name,size,pointer) int name[(size)]; pointer = name;
    

    The clarity of this one-liner is far lower than that of a two-line version from your post, so I would keep your initial version.

    EDIT (in response to the edit of the question)

    Another option is to create an unused pointer variable in a declaration, and assign your pointer in an initializer, like this:

    void func(void) {
        struct foo f;
        int n = ...;
        int tempArr1[n], *tempPtr1 = f.a = tempArr1;
        short tempArr2[n], *tempPtr2 = f.b = tempArr2;
        char tempArr3[n], *tempPtr3 = f.c = tempArr3;
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say for example, I have the following javascript function that returns a boolean:
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say that I have a set of relations that looks like this: relations
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say that I have a binary that I am building, and I include

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.