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

dbt in sizeof

5 posters

Go down

dbt in sizeof Empty dbt in sizeof

Post by SakeenaHarris Fri Mar 12, 2010 11:25 am

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main( int argv, char* argc[])
{
char* buffer = "hio happy dhhjikk";
printf("size is %d",sizeof buffer);
}
on running it gives
size is 8 bytes.. why?

SakeenaHarris

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

Back to top Go down

dbt in sizeof Empty Re: dbt in sizeof

Post by SakeenaHarris Fri Mar 12, 2010 11:37 am

printf("size is %zu", sizeof buffer);
this line prints correct 18

SakeenaHarris

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

Back to top Go down

dbt in sizeof Empty Re: dbt in sizeof

Post by Christopher Fri Mar 12, 2010 12:02 pm

even if i use %zu, am getting 8.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

dbt in sizeof Empty Re: dbt in sizeof

Post by mightyganesh Fri Mar 12, 2010 12:11 pm

@ sakina & christ!

i too get 8 as output even if i use %zu
mightyganesh
mightyganesh

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

http://ganeshguna.blogspot.com

Back to top Go down

dbt in sizeof Empty Re: dbt in sizeof

Post by itssrinath Fri Mar 12, 2010 12:12 pm

For me too.
Even tried in sh shell, same result.
Also tried "sizeof buff" and "sizeof(buff)", same results.

But isn't sizeof supposed to be used to determine the size of the data types alone and not the variable size?

itssrinath

Posts : 4
Points : 5
Join date : 2010-02-26

Back to top Go down

dbt in sizeof Empty Re: dbt in sizeof

Post by mightyganesh Fri Mar 12, 2010 1:01 pm

@ all! sakina has replaced char* buffer with char buffer[]
so only she got 18 as output!

and now my new doubt on this

what is the difference it creates by creating a string using

char* buffer=(char*)malloc(7);
buffer="ganesh";

and

char buffer[]="ganesh";
mightyganesh
mightyganesh

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

http://ganeshguna.blogspot.com

Back to top Go down

dbt in sizeof Empty Re: dbt in sizeof

Post by karthikeyan Fri Mar 12, 2010 1:04 pm

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main( int argv, char* argc[])
{

printf("size is %d %d %d",sizeof(int* ),sizeof(char* ),sizeof(void *));
}
answer:size is 8 8 8
in this sizeof refer to pointer
pointer stores address ,size is no of bytes the pointer uses for the address
in 32biit os the pointer size is 4,in 64bit the pointer size is 8

karthikeyan

Posts : 2
Points : 4
Join date : 2010-02-26

Back to top Go down

dbt in sizeof Empty Re: dbt in sizeof

Post by mightyganesh Fri Mar 12, 2010 1:17 pm

i have also given this in another thread !
its a copy paste of it!


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

dbt in sizeof Empty Re: dbt in sizeof

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


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