1

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;
    ...
}
5
  • It is more than ambiguous -- it will / can cause a memory fault if the memory has not been allocated at run-time or compile time. Commented Feb 22, 2012 at 13:44
  • Why do you need ptr? Why not just use arr directly? Commented Feb 22, 2012 at 13:45
  • @JamesMcLaughlin - he does not like how it looks. Commented Feb 22, 2012 at 13:50
  • Now that I also see your edit, I want to ask out of curiosity. Why do you need to accomplish this? Also as for a simpler way to do it, I see no problem with 2 lines. Two lines is not repetitive. You could follow the advice given by the people below and use a macro but a macro only for 2 lines as Hogan says creates more confusion than it's worth. Commented Feb 22, 2012 at 14:08
  • @update: is nothing wrong with it as long as the arrays don't go out of scope before the struct. (which is correct here) Commented Feb 22, 2012 at 14:37

4 Answers 4

1

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;
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

This seems like a clear case where you're in need of some refactoring. Take the similar statements, extract them into a new function (by passing a reference to the struct and the data you want the struct fields to point to) and give this new function a meaningful name.

This is probably more maintainable and readable than some fancy pointer arithmetic shortcut that you'll forget about in a few weeks or months.

Comments

0

The difference between ptr and arr in you example is you can change ptr's value. So I guess you want to move ptr through the array.

So how about this:

int arr[n], id=0;

And you change the value of id and use arr+id as ptr.

Comments

0

I guess the way to do this is to use a macro. Something like (untested)

 #define autoptr(name,size) int Arrayname[size]; name = Arrayname;

I'm not clear why this is helping I think it might "look ugly" but will be easier to maintain without the macro. In general, hiding what you are actually doing is a bad thing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.