C Programming - Arrays - Discussion

Discussion Forum : Arrays - General Questions (Q.No. 2)
2.
What does the following declaration mean?
int (*ptr)[10];
ptr is array of pointers to 10 integers
ptr is a pointer to an array of 10 integers
ptr is an array of 10 integers
ptr is an pointer to array
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
64 comments Page 1 of 7.

Apurva Nigam said:   1 decade ago
@Chris and @Sundar:

"printf("%d %d %d %d %d", a++, a--, ++a, --a, a); " evaluates the value passed in it from right to left(int turbo C).
That is
first "a" will get its value as 5
then --a will store 4
then ++a will store 5 (as "--a" had decremented a's value n ++a incremented it)
then "a--" will have 5 ( as a-- is post increment therefore a's value will be affected after evaluation of the expression "a--")
then "a++" will have value 4 (b'coz above "a--" has made its value 4)

Therefore output is: 45545
Hope u know preincrement increments the value first and then use it in the expression, whereas postincrement uses the previous value of the variable and then modifies it thet is increments by 1.

eg:
int x=5 ,y,z;
y= ++x; //first x will become 6 then y will get assigned 6
z= x++; //first x will get assigned in z that is z=6 then x gets incremented.

Good night :) :)
Hope u understood....

Ssk said:   1 decade ago
@Chris:
Turbo C uses __Cdecl__ type of Argument Passing mechanism in which rightmost arg is passed 1st and leftmost at atlast..
In printf(), as u might know, the leftmost format specifier is associated with leftmost value,but due to __Cdecl__, the evaluation of values is done from rightmost position..
So, 1st %d -> a++
2nd %d -> a--
3rd %d -> ++a
4th %d -> --a
5th %d -> a
This is binding is done,but as rightmost is evaluated 1st,
5th %d has value of a(5)
4th %d has --a(4) as preincrement has higher priority
3rd %d has ++a(5)
2nd %d has a--(5) as post increment is done after assignment
1st %d has a++(4) same reason as above..
So the output is as given..
Hope u have understood..
gd:)

Raju said:   1 decade ago
#include<stdio.h>
int main()
{
int a=5;
printf("%d %d %d %d %d", a++, a--, ++a, --a, a);
return 0;
}
when i compile this programme in gcc compiler the output as 4 5 5 5 5
because,programme excute from right to left so
a=5
--a predecrement so --a=4
++a preincrement so ++a=5
a-- postdecrement so actual value is 4,but first a value 5 is printed then it will decrement.
a++ postincrement so actual value is 5,but first a value 4 is printed then it will increment.
but all preincrements and predecrements get final a value 5 and postdecrements & postincrements doesn't change.
then the output is 4 5 5 5 5.

Sundar said:   1 decade ago
@Chris

The given output '4 5 5 4 5' is matches in Turbo C - 16 OS (DOS).


But, I tried in GCC, the details are as below:

Warnings:=

prog.c: In function 'main':
prog.c:5: warning: operation on 'a' may be undefined
prog.c:5: warning: operation on 'a' may be undefined
prog.c:5: warning: operation on 'a' may be undefined
prog.c:5: warning: operation on 'a' may be undefined

Output:=

4 5 5 5 5

Please can anyone explain with full details here. Thanks in advance.

Rahul kumar singh said:   1 decade ago
int (*ptr)[10] first we have to do the operation in bracket. So in bracket *ptr is there, notice the first variable name it is ptr then go right to the variable there is end bracket then move left of your variable indirection operator is there then again move towards right of ptr we get subscript so there it is array then left of ptr int is there. So ptr is a pointer to an array of 10 elements of type integer.
(1)

Ankit said:   9 years ago
@Chotu.

When we declare an array, say int a[10];

In above declaration 'a' is acting as a pointer because it holds the base address of array (the location from where it starts storing the values in memory continuously).

But in above example, they just declare the array name as pointer explicitly.

Hence, it is just same as the array declaration on 10 integers.

Prashanth said:   2 years ago
int ptr[10] means ptr is an array of 10 elements whose datatype is int.
int a;
int *ptr=a; means ptr is a pointer pointing to a which is an integer type.

(int *) ptr[10] means ptr is an array of 10 elements whose datatype is int and they all are pointers.

in (*ptr)[10] means ptr is a pointer which is pointing to 10 elements which are of int datatype.
(11)

LOL said:   1 decade ago
The brackets are the key to this problem. Always read C declarations from right to left (backwards) and start will stuff inside brackets.

[] = array * = pointer

int *ptr [10] backwards is [10] ptr * int

so this is Array of 10 pointers to int

int (*ptr)[10] bracket first and backwards is * ptr [10] int

so this is pointer to an array of 10 integers

Shruti said:   8 years ago
Given the declaration.

int *ptr[32];
break it down as

ptr -- ptr
ptr[32] -- is a 32-element array
*ptr[32] -- of pointers
int *ptr[32] -- to int.
If the declaration is:

int (*ptr)[32];
then it would break down as

ptr -- ptr
(*ptr) -- is a pointer
(*ptr)[32] -- to a 32-element array
int (*ptr)[32] -- of int.

Sasi said:   1 decade ago
int (*ptr) [10].

We can take this a cyclic check and everyone can read it easily by this way.

Start with ptr as it is a pointer go clock wise the next comes as array of size 10 that means ptr is a pointer to an array of 10 and the return type is int so it is finally as:.

ptr is a pointer to an array of 10 integers.


Post your comments here:

Your comments will be displayed after verification.