1

I'm a beginner of c and I'm not sure if I do it correctly.

char new_str[2000];
void ini(char a[]){
   for (int i=0;i<2000;i++) {
       a[i] = new_str[i];
   }
}

int main(void)
{
   char buf[2000];
   ini(buf);//initialize buf 
}

If I'm correct in this case, What if I change buf[2000] to buf[1000] or other number? Thanks in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
OKC
  • 181
  • 1
  • 4
  • 13

3 Answers3

1

if you want to find the size of the character array, you can use

sizeof(buf)/sizeof(buf[0])

Pass this value as the second argument to the ini() function.

So the code should be:

void ini(char a[], size_t size)
{
   for (int i=0; i<size; i++) 
   {
       a[i] = 0;
   }
}

int main(void)
{
   char buf[2000];
   ini(buf, (sizeof(buf)/sizeof(buf[0])));        //initialize buf 
}

This code will initialize the array to 0, irrespective of the array size.

Nithin Bhaskar
  • 686
  • 1
  • 5
  • 9
0

So if you want to init all the value to 0 you can directly initialize it:

char buf[2000] = {0};

you can't do it directly in your init function. But if you change your function ini to

#include <string.h>

void ini(char *a, size_t size){
  memset(a, 0, size);
}
Alexis
  • 2,149
  • 2
  • 25
  • 39
  • Thanks for answering. :) Can I do this in "ini" function? – OKC Jul 17 '13 at 13:12
  • do it in main() when you are first declaring buf[] – Nithin Bhaskar Jul 17 '13 at 13:13
  • 1
    This was not what OP was asking , A global variable is automatically initialized to 0 and through function ini he wants to initialize . – 0decimal0 Jul 17 '13 at 13:15
  • @NithinBhaskar Thanks for answering first, but I'm wondering is it possible initialize it in a function. Since the main ftn is depending on people who use your program and you'll never know whether they initialize it before. I just want to make sure whatever size of array they give me, I should be able to initialize it – OKC Jul 17 '13 at 13:17
  • @OKC you must then give the length of your array to the function and use memset or do the loop yourself. But if it's the user who give you the input at runtime you must check malloc – Alexis Jul 17 '13 at 13:18
  • @OKC if you want to initialize the array only in the ini(), and not in main(), you need to pass an additional argument to ini() to include the size of the array. In this case, ini() will initialize the array irrespective of its size, as long as you can pass the size of the array as an argument to it. – Nithin Bhaskar Jul 17 '13 at 13:28
  • Can someone tell me why am I down voted ? – Alexis Jul 17 '13 at 13:52
0

You are using the concept of global variables here. They are initialized to 0, So you can use it like this.

You are correct in this case except if you change buf[2000] to buf[1000] then you have to make some changes like this :

   for (int i = 0 ; i < 1000; i++) {// change i<2000 to  i<1000 otherwise you may get a segmentation fault error 
       a[i] = new_str[i];
   }

And yes, while dealing with passing arrays to a function you have to pass its size as described in other answers .:)

NOTE-- You are not initializing array with the ini function , You are actually assigning values to the array buf, and this answer is according to that. If you want to initialize the array then other answers are according well enough , otherwise make one more change if you are mistaken with initialization to assignment .

Comment in your main i.e,

ini(buf);// assign buf

My answer is according to that , if that is what you want to do :)

0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • 1
    Downvoters please care to explain and suggest :)..... are you a ghost mr. downvoter ... please explain :)....... hello .... are you there ? – 0decimal0 Jul 17 '13 at 13:53