PLEASE..

K

Keith Thompson

int main () {

typedef int array[5] = {1, 2, 3, 5 , 6};
array *ptr;

for (int i = 0; i < 5; ++i) {

printf ("%d", *ptr);
++ptr;
}

return 0;
}

why this is not operating .... as you said it is the pointer to the
array of 5 int.

It's "not operating" because it's not legal C, so it doesn't compile
in the first place.

You posted a chunk of code, but you didn't tell us what problem you
were having with it, or what you were trying to accomplish.

If you're asking why your compiler rejected the third line of your
program, please say so, and show us the error message, *and* tell us
what you're trying to accomplish.

Jens Toerring has made some suggestions. In addition, it looks like
you're trying to declare ptr as a pointer to an array, not as a
pointer to an int. That's almost certainly not what you should be
doing. Based on what you're trying to do in the loop, you want ptr to
be a pointer to int, so that *ptr is an int (which you can print with
"%d"), and so that incrementing it steps through the elements of your
array. If ptr is a pointer to an array of 5 ints, incrementing it
causes it step 5 ints at a time.
 
R

ranjeet.gupta

Better make that

int main( void )

o tell the compiler that main() does not take any arguments.

Yes I will follow the same i.e (int main (void))
typedef int array[5] = {1, 2, 3, 5 , 6};

Here you try to create a typedef for an array of 5 ints. You can't
assign any values in that context.

I was unaware of this, No doubt I used the typedef for the structures
in the header files (Declarartion) Thanks for catching my silly
error.
'ptr' is a pointer to an array of 5 ints. It's pointing nowhere yet.

Thansk for correcting me. (Really I would not have given the second
look
to the code before poating)
What you need is someting like

typedef int array[5];
array p = {1, 2, 3, 5 , 6};
int *ptr;

You also can't have

array *ptr = = {1, 2, 3, 5 , 6};

since here 'ptr' would be a pointer to an array of 5 ints, but on
the right hand side you would have an array and not the address
of such an array. (Except for literal strings there are no anony-
mous arrays in C like you have them e.g. in Perl where you can
say e.g.

my $p = [ 1, 2, 3, 5, 6 ];

and now $p is a pointer (reference) to an array with the specified
elements. And for string literals the support is rather restricted
since you can't change the contents of what the pointer is pointing
to.)

What you could do is

array p = {1, 2, 3, 5 , 6};
array *x = &p;

But you would still not be able to iterate over the elements of 'p'
using 'x' - after incrementing 'x' it would point to the memory
location directly after the 'p' array. Using a variable of such a
type would really make sense only if you had an array of arrays of
5 ints.

Sorry I did not get you, You mean to say we _cannot_ iterate over
the
elements of "p" using "x" ?? if not then what is the use of it ??
I think so I have messed it up with some thing.
 
J

Jens.Toerring

Sorry I did not get you, You mean to say we _cannot_ iterate over the
elements of "p" using "x" ?? if not then what is the use of it ??
I think so I have messed it up with some thing.

No, sorry, I don't think so. If 'x' is defined as above it's a
pointer to an int array with 5 elements - that's what you told
'array' to mean by using the typedef. And if you increment a
pointer to a certain type of object it always will point to the
next object of that type following the one it pointed to before
the increment. If you have an int pointer, pointing to an element
in an array of ints, then, after incrementing the pointer it
points to the next element in the array. If you have a pointer
to structure, pointing to an element of an array of structures,
it's going to point to the next structure in that array after
incrementing it. And if you have a pointer to an array of 5
ints, pointing to an element of an array of such arrays, it's
going to point to the next such array when you increment it,
not to an individual element in one of the arrays with 5 ints.
So, defining such a pointer and then incrementing it mostly
makes sense when you have an array of int arrays with 5 elements.

Such pointers sometimes can be useful - as an example take
an array of strings, all with a fixed length, e.g.

typedef char array[ 5 ];
array fl_str[ ] = { "Yes!", "No!!", "Quit" };
array *p = fl_str;

Now you can iterate over the strings in this array of char arrays
using

size_t i;
for ( i = 0; i < sizeof fl_str / sizeof *fl_str; i++ )
printf( "%s\n", *p++ );

but you can't access the individual chars in the strings using
this pointer. And if 'p' is a pointer to an array of 5 ints you
can't use it to point to the individual elements in such an
array, you need an int pointer to do that.

Regards, Jens
 
R

ranjeet.gupta

No, sorry, I don't think so. If 'x' is defined as above it's a
pointer to an int array with 5 elements - that's what you told
'array' to mean by using the typedef. And if you increment a
pointer to a certain type of object it always will point to the
next object of that type following the one it pointed to before
the increment. If you have an int pointer, pointing to an element
in an array of ints, then, after incrementing the pointer it
points to the next element in the array. If you have a pointer
to structure, pointing to an element of an array of structures,
it's going to point to the next structure in that array after
incrementing it. And if you have a pointer to an array of 5
ints, pointing to an element of an array of such arrays, it's
going to point to the next such array when you increment it,
not to an individual element in one of the arrays with 5 ints.
So, defining such a pointer and then incrementing it mostly
makes sense when you have an array of int arrays with 5 elements.

As far as I understood is that If i define the array as the type
def then it means the object is created of 5 ints (as in above case)
now I cant access the inner elements of the array.... right with the
declartion of the below
array *ptr;

now if I declare with the int *ptr = array; then i can access the
elemnets of the type def array ?? ...

beg me pardon if i had misunderstood something.

Thanks for your response and replies.
Regards
Ranjeet

Such pointers sometimes can be useful - as an example take
an array of strings, all with a fixed length, e.g.

typedef char array[ 5 ];
array fl_str[ ] = { "Yes!", "No!!", "Quit" };
array *p = fl_str;

Now you can iterate over the strings in this array of char arrays
using

size_t i;
for ( i = 0; i < sizeof fl_str / sizeof *fl_str; i++ )
printf( "%s\n", *p++ );

but you can't access the individual chars in the strings using
this pointer. And if 'p' is a pointer to an array of 5 ints you
can't use it to point to the individual elements in such an
array, you need an int pointer to do that.

Regards, Jens
 
J

Jens.Toerring

As far as I understood is that If i define the array as the type
def then it means the object is created of 5 ints (as in above case)

By just creating a typedef you don't have created an object, you just
have a new type. With this you now can start creating new objects of
this type.
now I cant access the inner elements of the array.... right with the
declartion of the below

Before this you had

typdef int array[ 5 ];
array *ptr;

Now 'ptr' is a pointer of type "pointer to an array of 5 ints".
now if I declare with the int *ptr = array; then i can access the
elemnets of the type def array ?? ...

No. What's

int *ptr = array;

supposed to mean? On the left hand side you have the definition of
an int pointer and on the right hand side a type. So that would be
like writing

int *ptr = char;

which obviously is a syntax error. If you want to initialize 'ptr'
you must put the address of an existing int (which could e.g. be
the address of the first element of an array) on the right hand side

If you want to access the elements of an int array (it doesn't
matter if you "hide" the fact that it's an array of ints behind
a typedef like yours) using a pointer you need an int pointer to
access the elements, no other type will do. So if you have

int foo[ 5 ];

or, using your typdef,

array foo;

then you need

int *bar = foo;

(making 'bar' point to the first element of 'foo' by this initialization)
to be able to do

for ( i = 0; i < 5; i++ )
*bar++ = 42;

That's something you can't do with a pointer defined like this

array *ptr = &x;

While 'ptr' and 'bar' point to the same location at first (a pointer
to an array points to the same place as a pointer to the first element
of that array), only 'bar' will point to the second element of 'x' after
an increment, while 'ptr' would point to the location directly after
the end of the array 'x' after a single increment.

Regards, Jens
 
R

ranjeet.gupta

As far as I understood is that If i define the array as the type
def then it means the object is created of 5 ints (as in above case)

By just creating a typedef you don't have created an object, you just
have a new type. With this you now can start creating new objects of
this type.
now I cant access the inner elements of the array.... right with the
declartion of the below

Before this you had

typdef int array[ 5 ];
array *ptr;

Now 'ptr' is a pointer of type "pointer to an array of 5 ints".
now if I declare with the int *ptr = array; then i can access the
elemnets of the type def array ?? ...

No. What's

int *ptr = array;

supposed to mean? On the left hand side you have the definition of
an int pointer and on the right hand side a type. So that would be
like writing

int *ptr = char;

which obviously is a syntax error. If you want to initialize 'ptr'
you must put the address of an existing int (which could e.g. be
the address of the first element of an array) on the right hand side

If you want to access the elements of an int array (it doesn't
matter if you "hide" the fact that it's an array of ints behind
a typedef like yours) using a pointer you need an int pointer to
access the elements, no other type will do. So if you have

int foo[ 5 ];

or, using your typdef,

array foo;

then you need

int *bar = foo;

(making 'bar' point to the first element of 'foo' by this initialization)
to be able to do

for ( i = 0; i < 5; i++ )
*bar++ = 42;

That's something you can't do with a pointer defined like this

array *ptr = &x;

While 'ptr' and 'bar' point to the same location at first (a pointer
to an array points to the same place as a pointer to the first element
of that array), only 'bar' will point to the second element of 'x' after
an increment, while 'ptr' would point to the location directly after
the end of the array 'x' after a single increment.

Jeans !! I am highly thankful that you provided me such a detailed
and expanatory reply to my doubt, At present I am still little bit
confused, So before shooting up, my another question to you I want to
perform some test on your e-mails, i am closely following your e-mail
to get it cleared. Thanks once again for guidence which you gave to me

which almost cleared my doubts.

Regards
Ranjeet
 

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

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top