dbt in sizeof
5 posters
dbt in sizeof
#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?
#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
Re: dbt in sizeof
printf("size is %zu", sizeof buffer);
this line prints correct 18
this line prints correct 18
SakeenaHarris- Posts : 7
Points : 13
Join date : 2010-03-08
Re: dbt in sizeof
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?
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
Re: dbt in sizeof
@ 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";
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";
Re: dbt in sizeof
#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
#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
Re: dbt in sizeof
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
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
Permissions in this forum:
You cannot reply to topics in this forum
|
|