Question!
-
My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }
-
My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }
Razanust wrote:
But the 'sizeof' command is returning the value '1'
It returns 1 and that is correct, because this is the size of a char type. If you need the count of the elements in array, you have to use
_countof
macro -> here.[^] This form here:count = _countof(ch)
will always return 100, because you actualy allocated memory for 100 elements on the stack. But note that the variable "n" from your code holds the count of the characters. ;)Razanust wrote:
count=sizeof(ch[100]);
Also note that your use of
sizeof
is wrong. If you need the size of an array in bytes, than you must change it to:sizeof(ch)
. Best wishes! :) Nurimodified on Tuesday, June 30, 2009 3:07 PM
-
My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }
If by 'whatever input' you mean the number you put in the square brackets for ch[100], I think this is because ch[n] is always going to be a pointer. It's equivalent to
char *ch = (char*)malloc(sizeof(char) * n);
then sizeof(ch) will always be 1 because it's pointing to the start of your array. In C, you sort of always have to keep track of the size of your arrays manually. -
My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }
OK - that's not how sizeof works - it tells you the size of a type, either via the typename ('sizeof (char)') or a compile-time static expression ('sizeof (3 * 4.0)'). The WIkipedia entry[^] is quite accurate. I presume you know you can use n to tell you how many characters have been entered...
Razanust wrote:
printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; }
Can I suggest you read up how strings (specifically null terminated strings) and arrays work in C?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
If by 'whatever input' you mean the number you put in the square brackets for ch[100], I think this is because ch[n] is always going to be a pointer. It's equivalent to
char *ch = (char*)malloc(sizeof(char) * n);
then sizeof(ch) will always be 1 because it's pointing to the start of your array. In C, you sort of always have to keep track of the size of your arrays manually.thebeekeeper wrote:
because ch[n] is always going to be a pointer
Nope. ch is a pointer, ch[n] is one character out of an array of characters, that is why its size is 1. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }
Razanust wrote:
count=sizeof(ch[100]);
As a side note, your code (possibly) does not own the memory at location
ch[100]
. While that variable holds 100 chars, they are numbered 0-99."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
thebeekeeper wrote:
because ch[n] is always going to be a pointer
Nope. ch is a pointer, ch[n] is one character out of an array of characters, that is why its size is 1. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
ha! I forgot that [] are for indexing into arrays!