Search code examples
cstringargument-passing

Why strings are permitted while arrays are not in this code?


#include<stdio.h>
void function1(){}
int main(void)
{
  function1(1,0.45,'b',"I am trying");
  function1();
  return 0;
}

this compiles nicely. But the below is showing

Error: use arr in function1 first....

Note that I am using code::blocks IDE and saved that file with .c extension.

#include<stdio.h>
void function1(){}
int main(void)
{
   function1(1,0.45,'b',"I am trying",arr[12]);
   function1();
   return 0;
}

sorry I made a mistake calling it an array. But {1,2,3,4} this is an array you will agree to it ..but this also does not work. Is it a bug or what ?


Solution

  • In the second case,

     function1(1,0.45,'b',"I am trying",arr[12]);
    

    arr[12] is a variable, and arr itself is not defined, least being as an array.

    In C, you need to define a variable before using it.

    FWIW,

    function1(1,0.45,'b',"I am trying");
    

    works, because

    • 1 is an int literal
    • 0.45 is a double literal
    • 'b' is a char literal
    • "I am trying" is a string literal

    and none of them is a variable.