C Programming - Arrays - Discussion
Discussion Forum : Arrays - Point Out Correct Statements (Q.No. 2)
                   
                                       
                                2.
Which of the following statements mentioning the name of the array begins DOES NOT yield the base address?
 
                                    | 1: | When array name is used with the sizeof operator. | 
| 2: | When array name is operand of the & operator. | 
| 3: | When array name is passed to scanf() function. | 
| 4: | When array name is passed to printf() function. | 
Answer: Option
                                                    Explanation:
                                                The statement 1 and 2 does not yield the base address of the array. While the scanf() and printf() yields the base address of the array.
Discussion:
20 comments Page 1 of 2.
                
                        Noel said: 
                         
                        7 years ago
                
                No, according to me,
Consider the code to follow. The scanf does NOT YIELD the base address, and the sizeof operator definitely does not give the base address.
#include<stdio.h>
int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%d\n", sizeof(arr));
printf("%d\n", &arr);
printf("%d\n", arr);
scanf(&arr);
printf("%d\n", *arr);
    
return 0;
}
                Consider the code to follow. The scanf does NOT YIELD the base address, and the sizeof operator definitely does not give the base address.
#include<stdio.h>
int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%d\n", sizeof(arr));
printf("%d\n", &arr);
printf("%d\n", arr);
scanf(&arr);
printf("%d\n", *arr);
return 0;
}
                     (1)
                
            
                        Manisha said: 
                         
                        9 years ago
                
                I agree @Nirupam.
Answer should be only A.
                Answer should be only A.
                     (2)
                
            
                        Jayram Yadav said: 
                         
                        9 years ago
                
                It is work fine by replacing statement 
scanf("%d", arr[i]); to scanf("%d", &arr[i]); in devc++
#include<stdio.h>
int main()
{
int size, i;
scanf("%d", &size);
int arr[size];
for(i=1; i<=size; i++)
{
scanf("%d", &arr[i]);
printf("p=%d\n", arr[i]);
}
return 0;
}
Output:
5
1
p=1
2
p=2
3
p=3
4
p=4
5
p=5
--------------------------------
Process exited after 10.07 seconds with return value 0.
Press any key to continue....
                scanf("%d", arr[i]); to scanf("%d", &arr[i]); in devc++
#include<stdio.h>
int main()
{
int size, i;
scanf("%d", &size);
int arr[size];
for(i=1; i<=size; i++)
{
scanf("%d", &arr[i]);
printf("p=%d\n", arr[i]);
}
return 0;
}
Output:
5
1
p=1
2
p=2
3
p=3
4
p=4
5
p=5
--------------------------------
Process exited after 10.07 seconds with return value 0.
Press any key to continue....
                        AKSHAY said: 
                         
                        9 years ago
                
                Well explained @Nirupam.
                
                        Nirupam said: 
                         
                        1 decade ago
                
                As of me the answer should be A only.
&a is a pointer to the array. It means it denotes the address of the first element of the array which is nothing but the BASE ADDRESS.
If we print the value of &a and &[0] in hex format, both the values are same.
E.g:
int a[3] = {0, 1, 2};
printf("%x %x", &a, &a[0]);
Then how can we say &a does not yield the base address of the array?
The same happens when an array is passed as an argument to printf() or scanf(). That means both the methods yield the base address of the given array.
                &a is a pointer to the array. It means it denotes the address of the first element of the array which is nothing but the BASE ADDRESS.
If we print the value of &a and &[0] in hex format, both the values are same.
E.g:
int a[3] = {0, 1, 2};
printf("%x %x", &a, &a[0]);
Then how can we say &a does not yield the base address of the array?
The same happens when an array is passed as an argument to printf() or scanf(). That means both the methods yield the base address of the given array.
                     (1)
                
            
                        Siraj said: 
                         
                        1 decade ago
                
                The answer is correct, first understand the question which is DOES NOT YIELD (means not giving base address). 
Hence the answer becomes C and D. Because A and B yields the base address of an array.
                Hence the answer becomes C and D. Because A and B yields the base address of an array.
                        Sbiz said: 
                         
                        1 decade ago
                
                considering, 
DOES NOT yield the base address.
The &a will always give the right numerical value.
The answer should include a and c;
Where does the idea that scanf will give the base address come from?
                DOES NOT yield the base address.
The &a will always give the right numerical value.
The answer should include a and c;
Where does the idea that scanf will give the base address come from?
                        Suraj kumar said: 
                         
                        1 decade ago
                
                @Adi :-is there any difference between base address and address of first element in an array ?
                
                        Isha said: 
                         
                        1 decade ago
                
                I can't understand &a will point to base address then why option B is not in answer.
                
                        PANKAJ said: 
                         
                        1 decade ago
                
                &a will give the base address.
@jasmin: base address and address of first element is same.
                @jasmin: base address and address of first element is same.
Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers