Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

here also it doesnt wrk

2 posters

Go down

here also it doesnt wrk Empty here also it doesnt wrk

Post by SakeenaHarris 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

Back to top Go down

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

Post by mightyganesh 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 : 35

http://ganeshguna.blogspot.com

Back to top Go down

Back to top


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