C Programming - Arrays - Discussion

2. 

What does the following declaration mean?
int (*ptr)[10];

[A]. ptr is array of pointers to 10 integers
[B]. ptr is a pointer to an array of 10 integers
[C]. ptr is an array of 10 integers
[D]. ptr is an pointer to array

Answer: Option B

Explanation:

No answer description available for this question.

Sagar Said said: (Aug 20, 2010)  
Can you explain me the concept

Chitra said: (Sep 1, 2010)  
*ptr is a pointer and [10] is array is array declaration.

Neha said: (Sep 10, 2010)  
Can you explain me the concept?

Ravikumar said: (Oct 1, 2010)  
We can use a * sometimes to declare the array both are same that is way answer is b.

Vijayalaxmi said: (Oct 3, 2010)  
Here we have to see the priority () have highest priority.

So int (*ptr)[] read it as pointer to array of integers.

If () is not there then int *ptr[] start with [] and it means array of pointers of type integers.

Avijit said: (Oct 28, 2010)  
ptr is a pointer,which is a type of int.ptr is an array.

Kuldeep Chaudhary said: (Nov 16, 2010)  
pointer of array is declared as type (*p)[14]
array of pointers is declared as type *p[14]

Gautam Jangra said: (Nov 26, 2010)  
int (*ptr)[10]
also we can write it as
int *ptr[10]

this line says that ptr is a pointer type variable which stores the address of first element of a 10 elements array list
as we know that array is continous memory type allocation.....

Neeraj Awasthi said: (Nov 28, 2010)  
If () is not there then int *ptr[] start with [] and it means array of pointers of type integers.

Nagaraj said: (Dec 30, 2010)  
The below program explains tat the starting address of the array. so it is [int (*ptr)[5];] is a pointer to array not array pointer which can be declared by int *ptr[5];
#include<stdio.h>
int main()
{
int arr[5];
int (*ptr)[5];
ptr=&arr[10];

printf("%ld",ptr[5]);
return 0;
}

Subaidha said: (Jan 21, 2011)  
* means its represent as a pointer variable and here within a array bracket they give us 10 integers.

So that the ans is B.

Balaji said: (Feb 23, 2011)  
Can you explain me the concept?

Purna Chandra said: (Feb 24, 2011)  
Yes p is pointer of 10 integers.

Ganesh said: (Apr 12, 2011)  
*ptr is a pointer and[10] is array declarations.

Lol said: (May 6, 2011)  
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

Chris said: (Jun 23, 2011)  
#include<stdio.h>
int main()
{
int a=5;
printf("%d %d %d %d %d", a++, a--, ++a, --a, a);
return 0;
}

// Ans : 4 5 5 4 5

Can any one explain the above code please ?

Sundar said: (Jun 23, 2011)  
@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.

Apurva Nigam said: (Jun 26, 2011)  
@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....

Raghu said: (Aug 9, 2011)  
Any one can explin main difference between a[] & *ptr[] with an example.

Sid... said: (Sep 1, 2011)  
*ptr (10) means.

It is a pointer type aaray whch have 10 integers in it.

Ssk said: (Sep 8, 2011)  
@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:)

Padmanaban said: (Sep 15, 2011)  
I have got that ++a is pre-increment so it will increment and then assign the value.

Similarly a++ means it assigns and then increment so its value cannot be printed by this only it shows the value what we assigned..... clear per-increment post-increment is the concept used there...!!!!!

Mayank Dixit said: (Sep 18, 2011)  
*ptr[10] means 'ptr is an array of pointer type 10 elements'.

(*ptr)[10] means there is an array of 10 elements with no array varaible but 'ptr is pointer type variable' that has base address of that array.

Vijay Kanth &Amp; Prasanth Krishna said: (Oct 19, 2011)  
int (*ptr)[10];
here "ptr" is name of array,[10] is index of that array.
but '*'-it denotes the pointer varable is created of specified name. name-(ptr).

Nitin Goyal said: (Nov 6, 2011)  
Here ptr is the pointer for the memory location occupied by array having 10 elements.

Gaurav said: (Jan 22, 2012)  
int (*arr)[10] /* It refers that arr is a pointer to an array of 10 integers*/
and,
int *arr[10] refers to an array of pointers which can hold the starting address of 10 different array of integer data type....

Divya said: (Jan 25, 2012)  
int (*ptr)[10];

Start reading what ever is in brackets 1st ,(ptr is a pointer)
Then go towards left till you hit ; (to an array of 10)
Then go backwards and read what ever is left out, (integers).

Goyal said: (Jan 29, 2012)  
int(*ptr)[10]

Vishwaschaurasiya said: (Jun 7, 2012)  
int (*ptr)[10];
ptr is a pointer, but this declaration is called pointer of an array..
Since,(*) this is unary oprator,known as (Astrick);

Sasi said: (Jun 30, 2012)  
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.

Srilakshmi said: (Jul 7, 2012)  
Array size is represented in []ie. , no. Of elements contained in it * represents for a pointer so ptr is a pointer to an array of 10 integers since the return type is specified as int.

Rajeev Tomar said: (Aug 30, 2012)  
Here [10] is the size of array (as in [], always size of array is represesnted) and *ptr is a pointer pointing the particular array. Means there in the memory, there is an array consisting of 10 blocks of integer type and a pointer is pointing to that particular array.

This array would be accessed by that pointer.

Raju said: (Sep 7, 2012)  
#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.

Disha Bhatt said: (Oct 13, 2012)  
Here size of an array is assinged to 10 and * has been used to declare pointer funtion so compiler will automatically consider it as an pointer funtion.Here it simply means it is a pointer funtion consisting of 10 elements.

Balu said: (Nov 19, 2012)  
First it will read the variable part i.e, (*ptr) and after that it will go anti-clock wise direction then it will read array part([]). Then it will go to before (*ptr)[10]. There is nothing so it will leave.

Amr Gadallah said: (Jan 19, 2013)  
char *ptr[10]; is an array of 10 pointers to characters.

char (*ptr)[10]; pointer to array of 10 characters.

Tharun said: (Mar 16, 2013)  
void main()
{
int (*d)[10];
d[0] = 7;
d[1]=10;
printf("%d\n",*d);
}

It should print 10 but compiler is showing error such as follows:

test.c:4:7: error: incompatible types when assigning to type \'int[10]\' from type \'int\'

Kumarachary said: (Aug 19, 2013)  
@Vijayalaxmi is right.

We have to see the priority () have highest priority.

So int (*ptr)[] read it as pointer to array of integers.

If () is not there then int *ptr[] start with [] and it means array of pointers of type integers.

Sanju said: (Aug 14, 2014)  
Here ptr is the pointer it is represented by using *...! then the symbol [] shows that it is an array and 10 implies the size of that array.

Sankar said: (Aug 18, 2014)  
If any one clear about ptrs please explain clearly. Because I don't understanding anything belong to this.

Vikash Kumar said: (Aug 26, 2014)  
As we know ptr holds the address of variables, and *ptr means what is the value of variables on that address.

Sornalatha said: (Sep 17, 2014)  
(*ptr) is a pointer. It point out the array of value [10]. So option B is correct.

Rahul Kumar Singh said: (Dec 31, 2014)  
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.

Yuvraj said: (Jun 27, 2015)  
Hey, I m not getting difference between array of pointer and pointer of array. Can you please explain me?

Abdul Quadir said: (Jun 30, 2015)  
Int (*ptr)[10];

It's a pointer to an array of 10 integers.

Notice that the array doesn't have a name of its own. We're using the pointer "ptr" to access the array.

The array will be created anywhere in the memory and the only way to access the array is via the pointer "ptr".

Chotu said: (Apr 15, 2016)  
Explain the answer of this question.

Ankit said: (Jun 13, 2016)  
@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.

Arman said: (Jul 22, 2016)  
int (*ptr)[10] - *ptr is a pointer to an array of 10 integers but how?

Because the size of the array is stored in [ ] this form and we seen the 10 is stored [10] like so explain it 10 integer or 10 sizes of the array.

Bhuvi said: (Apr 29, 2017)  
@ALL.

#include<stdio.h>
int main()
{
int a=5;
printf("%d %d %d %d %d", a++, a--, ++a, --a, a);
return 0;
}

This depends on the compiler, we don't know the order in which this expression is evaluated is either right to left or left to right.

Abhishek said: (Jul 13, 2017)  
Here, int (*ptr)[10]; - meaning ptr is a pointer to an array of 10 integers.

BUT what will be the NAME of the Array here(which array there is no name given to any array)?

Kishor said: (Aug 2, 2017)  
Please explain me.

Shruti said: (Aug 31, 2017)  
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.

Anonymous said: (Jun 14, 2018)  
Here [10] is Array of 10 integers and (*ptr) is pointer to point 10 Integers and store Addresses of 10 Integers

And here array name is ptr or same as pointer name.

Nawinraj said: (Jul 25, 2018)  
Pointer is only pointer to assign array functionto 10 integers.

Ishtha said: (Sep 6, 2018)  
I can't get your points. Explain more please.

Jay said: (Mar 25, 2019)  
Could you please explain the question.

Abhishek Prajapati said: (Mar 27, 2019)  
As we write ' int a[10] ' then we call it:- a is an array of 10 integer type elements.

Like that we can say in " int (*ptr)[10] "ptr (due to * ptr is pointer variable) is a pointer to an array of 10 integer type elements.

Rupa said: (Dec 26, 2019)  
Could you please explain the question?

Siva said: (Dec 29, 2019)  
ptr is a pointer variable that can store an array of integer values(i.e. 10 integer values).

Ashik said: (Oct 31, 2020)  
Please explain in detail.

Teja said: (Nov 6, 2020)  
ptr is a pointer which points an array.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.