here also it doesnt wrk

Go down

here also it doesnt wrk Empty here also it doesnt wrk

Post by SakeenaHarris on Fri Mar 12, 2010 12:57 pm

cat fileone.c
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

int main( int argc, char* argv[])
{
char* buffer;
read(0,buffer,50);
printf("size is %d", sizeof buffer);
return 1;
}

[337480@oracleclient ~]$ gcc fileone.c -o nnn.out
[337480@oracleclient ~]$ ./nnn.out
thuiid kjkl
size is 8[337480@oracleclient ~]$

SakeenaHarris

Posts : 7
Points : 13
Join date : 2010-03-08

View user profile

Back to top Go down

here also it doesnt wrk Empty Re: here also it doesnt wrk

Post by mightyganesh on Fri Mar 12, 2010 1:15 pm

hey i got the solution!

the size of char* datatype is 8 bytes!

so when ever we try to print the sizeof buffer we get the output as 8

now try printing the sizeof *buffer (buffer is pointer and * - Astrix before pointer gives the value of the location it point to!)
it will print 1 (which is the sizeof char datatype)

but when we try printing sizeof buffer[] we get the length of array! and not the size of char datatype (i.e. 1)

(pointer just points to a memory location! but array allocates a sequence of memory and creates a pointer which points to the first memory address in the sequence of allocated memory)

thats the difference between array and pointers
mightyganesh
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 31

View user profile http://ganeshguna.blogspot.com

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum