Thursday 2 October 2014

Tricky C Programs

Hello friends,

Here i have given some basic programs to understand pointer to array and pointer to string.
copy each and run it on your machine one bye one and see the difference between each of them.

1. Array with Pointer
* Predict the output of following programs.

1.1
main()
{
int arr[]={10,20,40,50,45};
int i;
for(i=0;i<5;i++)
printf("%d ",*(arr+i));
}
-------------------------------------------------------------------
many people have question, what is the difference between array name and pointer to that array?
Run program 1.2 and 1.3 and get the answer by your self.

1.2
main()
{
int arr[]={10,20,40,50,45};
int i;
for(i=0;i<5;i++)
printf("%d ",*(arr++));
}
-------------------------------------------------------------------
1.3
main()
{
int arr[]={10,20,40,50,45},*ptr;
int i;
ptr = arr;
for(i=0;i<5;i++)
printf("%d ",*(ptr++));
}
--------------------------------------------------------------------
1.3 and 1.4 shows the importance of bracket ' ( ) ' .

1.4
main()
{
int arr[]={10,20,40,50,45},*ptr;
int i;
ptr = arr;
for(i=0;i<5;i++)
printf("%d ",(*ptr)++);
}
---------------------------------------------------------------------
1.5
main()
{
int arr[]={10,20,40,50,45},*ptr;
int i;
ptr = arr;
for(i=0;i<5;i++)
printf("%d ",*ptr++);
}

--------------------------------------------------------------------------------------------------

2. String and Pointer :

2.1
void main()
{
char arr[] = "hello world !!!";
printf("%s\n",arr);
}
------------------------------------------------------------------------
2.2
void main()
{
char *ptr;
ptr = "hello world !!!";
printf("%s\n",ptr);
}
------------------------------------------------------------------------
2.3
void main()
{
char *ptr;
scanf("%s",ptr);                             // input any string.
printf("%s\n",ptr);
}
------------------------------------------------------------------------------------------
In program 2.3 you will get segmentation fault during run time. why?

because when you compile your program compiler decides howmuch space this program will require for execution and for storing variables and all.

In 2.1 and 2.2 compiler automatically calculates the space for given string because the string is already given. but in 2.3,we are entering string during run time and compiler didn't give us any space to store our string. so during run time it gives segmentation fault. In this case we have to use dynamic memory allocation. see example 2.4....
------------------------------------------------------------------------------------------
2.4
void main()
{
char *ptr; = (char *)malloc(5*sizeof(char));
scanf("%s",ptr);                             //  input "hello" or any string with max 5 char.
printf("%s\n",ptr);
}
----------------------------------------------------------------------

i think, these programs are enough to understand pointer to array and pointer to string.
for any query or explanation please use comment box.

Are you a Fresh Programmer?? Join facebook group to be a perfect programmer.
https://www.facebook.com/groups/121611948548929/

Link for Pointer in C :
http://letsmakeceasy.blogspot.in/2014/09/pointer-in-c.html

Link for Array in C :
http://letsmakeceasy.blogspot.in/2014/09/array-in-c.html

Link for Preprocessor in C :
http://letsmakeceasy.blogspot.in/2014/09/preprocessor-directives.html

Thanks
Akash Patel. 

2 comments:

  1. hii
    i tried all programs.
    In 2nd program of array to pointer (1.2) There will be an error of Lvalue required.
    Can you please explain program 1. 3 1.4 1.5
    It will give same output.
    Then what is difference?
    are *(ptr++) , *ptr++ , *(ptr)++ same??
    Can you please give me your whatsapp no ?
    Add me on Dhruvin shah 9537981467
    Please do reply.

    ReplyDelete
  2. sorry for the late.

    you ask wrong question,
    are *(ptr++), *ptr++, *(ptr)++ same??
    it should be,
    are *(ptr++), *ptr++, (*ptr)++ same?? //(*ptr++) not *(ptr)++.

    first two are same but third one will give different output.
    it all about precedence. parentheses has highest precedence.

    suppose we have integer array -> 10 , 20 , 40 , 50 , 45.
    their addresses respectively -> 1000 ,1004 , 1008 , 1012 , 1016.

    and ptr = 1000, means it points to value 10.
    so if we do *(ptr++) -> (ptr++) first increment pointer so ptr will now point to 1004.
    and *(ptr++) will give value 20.

    same for *ptr++.

    but when we write (*ptr)++ -> ptr = 1000.
    *ptr = 10.
    so, (*ptr)++ = 11.

    run third program again, you will get 11,12,13,14,15.

    in example 1.2, we are doing arr++. here arr works like pointer to given array, but at the end it is not pointer so we cann't do all operations that we can do with pointer.

    so in our prog "ptr = arr". and we can do ptr++ and ptr will point to next pointer but we cant do arr++.

    for more, visit pointer in c. and refer pointer to array.

    ReplyDelete