Jump to content

Getting the size of an unknown array in C

- - - - -

  • Please log in to reply
8 replies to this topic

#1
ic3m4n

ic3m4n

    Newbie

  • Members
  • Pip
  • 9 posts
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:


#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

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

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
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
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.

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

AdvMutant said:

char ch[20] = 'c'; // We need a terminating null..
You are assigning a char constant to a char array, that doesn't compile

AdvMutant said:

char ch [20]= "c";
Correct, because it is initialized with null terminators: chr[20] = "c" == 0x64 0x00 0x00 0x00 ... 0x00 0x00 0x00
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.

#5
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
Yeah, I just tested it and it won't run. Sorry :c-lol:
Well, then the following will -
char ch[20]; // We need a terminating null..
ch[0] = 'c'; ch[1] = '\0';

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#6
ic3m4n

ic3m4n

    Newbie

  • Members
  • Pip
  • 9 posts
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
Muted

Muted

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

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.
Hello, ic3m4n.

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

#8
ic3m4n

ic3m4n

    Newbie

  • Members
  • Pip
  • 9 posts
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
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
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.

Posted Image
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