473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

Initialization of integer pointers

Why int *k={1,2,3} illegal in C?
Nov 14 '05 #1
6 1454
"aruna" <ar********@yahoo.co.in> wrote in message
news:a2**************************@posting.google.c om...
Why int *k={1,2,3} illegal in C?


Because it is. This, however, is not:

int array[] = {1, 2, 3};
int *k = array;
Nov 14 '05 #2
aruna wrote:

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


What does {1,2,3) point to? I have grave doubts that it is to an
integer of any flavor.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3
In message <a2**************************@posting.google.com >
ar********@yahoo.co.in (aruna) wrote:
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.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #4

"Kevin Bracey" <ke**********@tematic.com> a écrit dans le message de
news:e1****************@tematic.com...
In message <a2**************************@posting.google.com >
ar********@yahoo.co.in (aruna) wrote:
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
Nov 14 '05 #5
>> In message <a2**************************@posting.google.com >
ar********@yahoo.co.in (aruna) wrote:
Why int *k={1,2,3} illegal in C?
"Kevin Bracey" <ke**********@tematic.com> a écrit dans le message de
news:e1****************@tematic.com...
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 article <c6**********@news-reader3.wanadoo.fr>
jacob navia <ja***@jacob.remcomp.fr> writes: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[i] = 72;
ip2[j] = -9;
...
}

Here the assignemnt to ip1[i] 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).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6
ar********@yahoo.co.in (aruna) wrote in message news:<a2**************************@posting.google. com>...
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.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Makis Papapanagiotou | last post by:
Hello all, There is a strange case where a class Test is composed from three objects of other classes. i.e. class Test { public: Test(); private: Point x;
13
by: Jayw710 | last post by:
hi all, this should be basic, but I can't seem to get the right syntax. i have a struct struct PTStruct { int x; int y;
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
12
by: natkw1 | last post by:
Hi, I'm attempting to understand the use of pointers(at least grasp how pointers work). I've read the FAQ on http://www.eskimo.com/~scs/C-faq/s6.html on pointers and arrays but I'm still a bit...
8
by: Tony Young | last post by:
Hi, The following code will have a compile error unless I remove "int *p" from class D (the error message is attached below). But I do need the *p. Someone suggested to change the...
14
by: gustavo | last post by:
I was looking at the Sendmail's source code, and i've got confused about this kind of initialization: ------------------------ struct prival PrivacyValues = { { "public", PRIV_PUBLIC }, {...
49
by: Luke Meyers | last post by:
Lately I find myself increasingly preferring the practice of going ever-so-slightly out of my way to avoid the use of the form of initialization which uses the '=' symbol, on the grounds that it...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
7
by: krishna | last post by:
What is the need of this syntax for initializing values, isn't this ambiguous to function call? e.g., int func_name(20); this looks like function call (of course not totally as there is no...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.