Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

c - Declare and initialize pointer concisely (i. e. pointer to int)

Given pointers to char, one can do the following:

char *s = "data";

As far as I understand, a pointer variable is declared here, memory is allocated for both variable and data, the latter is filled with data and the variable in question is set to point to the first byte of it (i. e. variable contains an address that can be dereferenced). That's short and compact.

Given pointers to int, for example, one can do this:

int *i;
*i = 42;

or that:

int i = 42;
foo(&i); // prefix every time to get a pointer
bar(&i);
baz(&i);

or that:

int i = 42;
int *p = &i;

That's somewhat tautological. It's small and tolerable with one usage of a single variable. It's not with multiple uses of several variables, though, producing code clutter.

Are there any ways to write the same thing dry and concisely? What are they? Are there any broader-scope approaches to programming, that allow to avoid the issue entirely? May be I should not use pointers at all (joke) or something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

String literals are a corner case : they trigger the creation of the literal in static memory, and its access as a char array. Note that the following doesn't compile, despite 42 being an int literal, because it is not implicitly allocated :

int *p = &42;

In all other cases, you are responsible of allocating the pointed object, be it in automatic or dynamic memory.

int i = 42;
int *p = &i;

Here i is an automatic variable, and p points to it.

int * i;
*i = 42;

You just invoked Undefined Behaviour. i has not been initialized, and is therefore pointing somewhere at random in memory. Then you assigned 42 to this random location, with unpredictable consequences. Bad.

int *i = malloc(sizeof *i);

Here i is initialized to point to a dynamically-allocated block of memory. Don't forget to free(i) once you're done with it.

int i = 42, *p = &i;

And here is how you create an automatic variable and a pointer to it as a one-liner. i is the variable, p points to it.

Edit : seems like you really want that variable to be implicitly and anonymously allocated. Well, here's how you can do it :

int *p = &(int){42};

This thingy is a compound literal. They are anonymous instances with automatic storage duration (or static at file scope), and only exist in C90 and further (but not C++ !). As opposed to string literals, compound literals are mutable, i.e you can modify *p.

Edit 2 : Adding this solution inspired from another answer (which unfortunately provided a wrong explanation) for completeness :

int i[] = {42};

This will allocate a one-element mutable array with automatic storage duration. The name of the array, while not a pointer itself, will decay to a pointer as needed.

Note however that sizeof i will return the "wrong" result, that is the actual size of the array (1 * sizeof(int)) instead of the size of a pointer (sizeof(int*)). That should however rarely be an issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...