Arrays are used to store multiple values in a single variable instead of declaring separate variables for each value.
All the values stored in an array must be of same data type.
Internally array representation gets converted into pointer representation.
intarr[5]; // Here arr is a constant integer pointer
arr[idx] =*(arr + idx) =*(idx + arr) =idx[arr];
What is the output of the following C code?
#include<stdio.h>
intmain()
{
intarr[5] = {1, 2, 3, 4, 5};
for (int i =0; i <5; i++)
printf("Address of %d idx element: %p\n", i, arr + i);
}
Address of 0 idx element: 000000189e5ff650
Address of 1 idx element: 000000189e5ff654
Address of 2 idx element: 000000189e5ff658
Address of 3 idx element: 000000189e5ff65c
Address of 4 idx element: 000000189e5ff660
What is the output of the following C code?
#include<stdio.h>
intmain()
{
intarr[5] = {1, 2, 3, 4, 5};
for (int i =0; i <5; i++)
printf("Value of arr[%d]: %d = %d = %d = %d\n", i, arr[i], *(arr + i), *(i + arr), i[arr]);
}
Value of arr[0]: 1 = 1 = 1 = 1
Value of arr[1]: 2 = 2 = 2 = 2
Value of arr[2]: 3 = 3 = 3 = 3
Value of arr[3]: 4 = 4 = 4 = 4
Value of arr[4]: 5 = 5 = 5 = 5
intarr[5];
int*ptr = arr;
arr = Array name = constant pointer ptr = Pointer name = variable pointer
Then we can't change the value of arr i.e., arr will point to a address which can't be changed, but we can change
the value of ptr.
Valid statements
Invalid statements
ptr = #
arr = #
ptr++;
arr++;
ptr = ptr - 1;
arr = arr - 1;
Pointer to an Array
intarr[5] = {1,2,3,4,5};
int (*arrPtr)[5] =&arr;
Now, arrPtr will behave same as arr, but we can change it unlike arr.
What is the output of the following C code?
#include<stdio.h>
intmain()
{
inta[10];
printf("%d", *a +1-*a +3);
}
4
What is the output of the following C code?
#include<stdio.h>
char*getString()
{
char str[]="Deepak Chandra Sharma";
return str;
}
intmain()
{
printf("%s", getString());
return0;
}
warning: function
returns address of local variable return str; (null)
What is the output of the following C code?
#include<stdio.h>
intmain()
{
int arr[]= {1, 2, 3};
int*p = arr;
if (&p == arr)
printf("Same");
else
printf("Not same");
}
warning:
comparison of distinct pointer types lacks a cast if (&p == arr) Not same
What is the output of the following C code?
#include<stdio.h>
intmain()
{
int arr[]= {1, 2, 3};
int*p = arr;
if (p == arr)
printf("Same");
else
printf("Not same");
}
Same
Strings
a string is a sequence of characters that are stored in contiguous memory locations.
Strings in C are represented as arrays of characters, terminated by a null character '\0'.
char str[]="abc";
str is a string.
char*strPtr ="abc";
strPtr is a string constant or string literal.
What is the output of the following C code?
#include<stdio.h>
intmain()
{
char str[]="abc";
str[1] ='1';
printf(str);
char*ptr ="abc";
ptr[1] ='p';
printf(ptr);
}
a1c Explanation: ptr is a string constant, so we
can't change it's value. So when we tried to do so, we get an segmentation fault exception.
What is the output of the following C code?
#include<stdio.h>
intmain()
{
char arr[]="deep";
char*ptr = arr;
while (*ptr !='\0')
++*ptr++;
printf("%s%s", arr, ptr);
}
effq Explanation: ptr points to the same address to
which arr points. Firstly, the pointer ptr will be dereferenced, then its value will be pre-incremented
and then the pointer will be post-incremented.
What is the output of the following C code?
#include<stdio.h>
intmain()
{
char str[]="abc" "pqr""abc";
printf(str);
}
abcpqrabc Explanation: If we try to assign two
string in a char array like above, then they will concatenate to a single character array.
What is the output of the following C code?
#include<stdio.h>
intmain()
{
char str[]="geeks\nknowledge";
char*ptr1, *ptr2;
ptr1 =&str[3];
ptr2 = str +5;
printf("%c", ++*str ---*ptr1 +*ptr2 +2);
printf("%s", str);
}
'\n'
heejs
knowledge Explanation: After evaluating the expression inside first printf, we will get '\n' i.e.,
next line. Because ptr1 and ptr2 points to the same string, so the original string also changes.
What is the output of the following C code?
#include<stdio.h>
intmain()
{
char str[]="Deepak";
char*s1 = str, *s2 = str;
int i;
for (i =0; i <7; i++)
{
printf("%c", *str);
++s1;
}
for (i =0; i <6; i++)
{
printf("%c", *s2);
++s2;
}
}
DDDDDDDDeepak Explanation: s1, s2 and str points to
the same string but all three are different pointers. So pre-incrementing s1 doesn't mean that we are
incrementing str pointer.
char str[]= {'a', 'b', 'c'};
If we don't add '\0' as the last element of character array like above, then our string will get appended by some
garbage value. But if we also give the size while initializing, then if there are 3 elements (excluding '\0') and we give size 4 or more, then it will be fine, but if we give size less than 4, it will add up some garbage values.
But if we declare a string using double quotes, then there is no need to add '\0' at the end, compiler
automatically adds that.
Comments
Post a Comment