#include <stdio.h>
#include <stdlib.h>
char * getNumber()
{
char *number;
int i;
number = (char*)calloc(50,sizeof(char));
for(i=0; i<49; i++)
{
number[i] = fgetc(stdin);
if(number[i] == '\n')
{
number[i] = '\0';
break;
}
}
}
int main(int argc, char* argv[])
{
char *num;
int n;
num = (char*) calloc(50, sizeof(char));
printf("Give me a number: ");
num = getNumber();
n = sizeof(*num)/sizeof(char);
printf("Your number has %d digits\n", n);
return 0;
}
F.Y.I. I am using VIM and GCC
8 replies to this topic
#1
Posted 11 December 2010 - 11:21 PM
Hello all, I am currently making a temperature converter, and am in the process of working out the bugs of one of my functions. One of the things I need to do in order to get the function working properly, is to get the size of a char array which is filled by input to the user. How do I do this? Essentially what I want to be able to do is take a number as input from the user and figure out how many digits are in the number. The array must be of type char because there is additional info included in the input. I have this bit of the code in a separate file trying to get it to run so it will work in the larger program. This is what I have thus far:
|
|
|
#2
Posted 12 December 2010 - 01:03 AM
Hi,
I notice a mistake in your function
you're returning nothing from this function
so the correction should be like
Now since we always put '\0' after the last character input by the user: so you can determine the size of array as follow
I hope this helps!
Munir
I notice a mistake in your function
char * getNumber()
{
char *number;
int i;
number = (char*)calloc(50,sizeof(char));
for(i=0; i<49; i++)
{
number[i] = fgetc(stdin);
if(number[i] == '\n')
{
number[i] = '\0';
break;
}
}
}
you're returning nothing from this function
so the correction should be like
char * getNumber()
{
char *number;
int i;
number = (char*)calloc(50,sizeof(char));
for(i=0; i<49; i++)
{
number[i] = fgetc(stdin);
if(number[i] == '\n')
{
number[i] = '\0';
return number;
}
}
//we make sure that the last correct is string terminator character.
number[49] = '\0';
return number;
}
Now since we always put '\0' after the last character input by the user: so you can determine the size of array as follow
int sizeofArray = strlen(num);
I hope this helps!
Munir
#3
Posted 12 December 2010 - 04:57 AM
You don't always need \0.
char ch[20] = 'c'; // We need a terminating null. char ch [20]= "c"; // The computer reads it like 'c' and adds '\0' automatically.Also, Keep in mind that strlen() is an ANSII C functions, and you have to include string.h to use it.

There is no problem that cannot be solved by the use of high explosives.
#4
Posted 12 December 2010 - 05:06 AM
AdvMutant said:
char ch[20] = 'c'; // We need a terminating null..
AdvMutant said:
char ch [20]= "c";
if not initialized or memset'd it requires null termination to verify array bounds at execution time.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 12 December 2010 - 06:43 AM
Yeah, I just tested it and it won't run. Sorry :c-lol:
Well, then the following will -
Well, then the following will -
char ch[20]; // We need a terminating null.. ch[0] = 'c'; ch[1] = '\0';

There is no problem that cannot be solved by the use of high explosives.
#6
Posted 12 December 2010 - 10:47 AM
Thanks, I've already gotten it solved, and I noticed that I didn't return anything in my function lol, this is because I was actually calling it from a custom header file, but I didn't want to confuse you guys so I re-wrote it really quick and just forgot the return.
#7
Posted 12 December 2010 - 10:42 PM
ic3m4n said:
Thanks, I've already gotten it solved, and I noticed that I didn't return anything in my function lol, this is because I was actually calling it from a custom header file, but I didn't want to confuse you guys so I re-wrote it really quick and just forgot the return.
Didn't you post his on another forum...? Hack Forums
I do believe I answered you there... I've nothing against posting on multiple forums, just curious though.
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills
- Beverly Sills
#8
Posted 14 December 2010 - 04:57 PM
Yes Muted, I also posted this over there, wasn't sure where I would get the quicker answer, and wanted to have a wider community of people to help me. Also, thanks for the help.
#9
Posted 16 December 2010 - 05:45 AM
Hey guys. I was just messing with pointers to pointers and pointers to multi-dimensional arrays, and I found a way to determine the size of an array of any time. Here's the code -
int y[40], size; size = sizeof(y) / sizeof(int);You get the total size of an array in bytes, and divide it by the size of an element, in bytes. The result is the number of elements in an array. It doesn't work with pointers[like strlen()], but it's still useful. The only reason I posted it is because I saw some guy on another site saying that "it's impossible to get the size of a non-character array in C", so I proved it is.

There is no problem that cannot be solved by the use of high explosives.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









