Initialization of integer pointers

K

Kevin Bracey

Why int *k={1,2,3} illegal in C?

Because. In C99 you can do:

int *k = (int []) {1,2,3};

which may be what you intended by your code - it creates an unnamed
int array and initialises k to point to it.

In pre-C99 compilers you can do:

int k[] = {1,2,3};

for a similar effect. Even though k is an array, not a pointer, you will be
able to use k in pretty much the same way, due to the way references to
arrays usually decay into a pointer.
 
J

jacob navia

Kevin Bracey said:
In message <[email protected]>
Why int *k={1,2,3} illegal in C?

Because. In C99 you can do:

int *k = (int []) {1,2,3};

which may be what you intended by your code - it creates an unnamed
int array and initialises k to point to it.

Why is that

char *a = "abc";

is legal then?

At least C99 improved by allowing a cast.
Couldn't we drop the cast too?

It confuses beginners.

jacob
 
C

Chris Torek

Kevin Bracey said:
Because. In C99 you can do:
int *k = (int []) {1,2,3};
which may be what you intended by your code - it creates an unnamed
int array and initialises k to point to it.

Why is that
char *a = "abc";
is legal then?

This has always been fine, of course.

What is odd is not that:

ptr_to_char = "string literal";

is allowed, but rather that:

char array[] = "string literal";

is allowed. It is this form, not the string-literal-as-anonymous-array,
that is the exception in C. All other array initializers require
a sequence of values that map to the right type:

int ai[] = { 1, 2, 3, 0 };
double ad[] = { 1.0, 2.0, 3.0, 0 };
char ac[] = { '1', '2', '3', 0 };
At least C99 improved by allowing a cast.
Couldn't we drop the cast too?

It is not a cast; it just looks like one. The point of the type-name
in parentheses is to supply the type for the array. When the code
above initializes the arrays "ai", "ad", and "ac", the type comes
from the array -- which is why we can also write:

double ad2[] = { 1, '2', 3L, 0.0f };

to get ad2[0] set to 1.0, ad2[1] set to 50.0 on ASCII machines, and
so on. What would you propose do to with, e.g.:

variadic_function( { 'a', 3.14159, 42L } );

if there were no type-hint? (There *are* languages that can handle
the "obvious" meaning of the above, in which there is little
difference between arrays and structures, but they are not C.)

If the array type in parentheses is const-qualified, the C99
anonymous array may be located in read-only storage and there can
be just the one copy. If not, it must be read/write and the copy
must be re-created upon entry to a block (modulo any optimization
allowed via the "as-if" rule). For instance:

void f(void) {
int i, j;
int *ip1, *ip2;
int a1[] = { 1, 2, 3 };

ip1 = a1;
ip2 = (int []) { 4, 5, 6 };
...
/* at this point, assume i and j are both in the [0..2] range */
ip1 = 72;
ip2[j] = -9;
...
}

Here the assignemnt to ip1 changes one of the elements of the
array a1, and the assignment to ip2[j] changes one of the elements
of the anonymous array that might be named a2 if we had given it
a name. When f() returns and is called again later, a1[] and what
might have been called a2[] have to be restored to their original
conditions ({1,2,3} and {4,5,6} respectively).
 
H

hugo27

Why int *k={1,2,3} illegal in C?

Presently reading C by Example (Que).
'Tis a rare book from which one can learn nothing at all.

We know this is proper:
int *k[3];
Creates a 3 element pointer variable array.
Suppose three integer variables,
int cnt1 = 1;
int cnt2 = 2;
int cnt3 = 3;
We know we can initialize pointer elements indivdually like so:
k[0] = &cnt1;
k[1] = &cnt2;
k[2] = &cnt3;
The & asigns "address of" variable to the pointer.
aruna, don't asign small integers to a pointer.
The pointer will then point to low memory and you could
overwrite vital BIOS or Interupt services.

Might we then, gentalmen, declare and initialize in one statement:
int *k[3] = {&cnt1, &cnt2, &cnt3};
(Y/N)?

Further, we have the case of a larger number of variables,
say 100, e.g.the ints from 1 to 100. As Alex demonstrates,
one can asign the array name(by itself) to a single pointer
and reference array elements by pointer arithmetic.
But let's say you insist on a unique pointer for each variable.
We could try this:
int num[100];
int *k[100];
int ctr;
for(ctr=1; ctr<=100; ctr++)
{nun[ctr-1] = ctr;
k[ctr-1] = &nun[ctr-1];
}
Then *k[index] points to num[index].
Note here that the array name, nun, by itself, is a
pointer constant. You cannot asign anything to it:
nun = 15; /* Invalid */
But the elements, nun[index], behave just like ordinary variables.

hugo27, member, experts-exchange.com, April 30, 20004.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top