3

I'm wondering something like this is possible:

// declaration
void func();

int main()
{
    int ar[] = { 1, 2, 3 };
    func(ar); // call with parameter
    return 1;
}

void func() // no parameters
{
    // do something
}

Can someone explain me this and especially how can I access ar in func()?

4
  • This program will not compile. Commented Oct 7, 2014 at 8:42
  • 6
    @mahendiran.b your compiler is broken then Commented Oct 7, 2014 at 8:44
  • @mahendiran.b this program compile! Commented Oct 7, 2014 at 9:02
  • thanks @leon22. Generally if the function is not taking any arguments means void will be used. But in your case in func declaration, the argument stands nothing. So it not giving compilation error. In my 1st read i dint check the declaration statement properly, so I given wrong comment. Commented Oct 7, 2014 at 9:06

6 Answers 6

10

In C (not C++), a function declared as func() is treated as having an unspecified number of untyped parameters. A function with no parameters should be explicitly declared as func(void).

Sign up to request clarification or add additional context in comments.

4 Comments

even this signature func(void) is also getting invoked. i just can't believe it.
@Rustam Just tried and it did not compile (gcc 4.8.2, without any flags, just -o): error: too many arguments to function ‘func’
@Rustam But your definition differs from the declaration: func() vs func(void). The compiler looks at the declaration. See: ideone.com/Ah9XbE :)
This leaves me wondering why func(void) is not equivalent to func(). What is the purpose of being able to call functions with arguments that they cannot access?
3

A hack would be to exploit the GCC calling convention.

For x86, parameters are pushed into stack. Local variables are also in the stack.

So

void func()
{
   int local_var;
   int *ar;
   uintptr_t *ptr = &local_var;
   ptr += sizeof(int *);
   ar = (int *)ptr;

May give you the array address in ar in x86.

For x86_64, the first parameter is stored in rdi register.

void func()
{ 
    uintptr_t *ptr;
    int *ar;
    asm (
    "movq %%rdi, %0"   
    :"=r"(*ptr)
    :
    :"rdi");
    ar = (int *)ptr;

May give you the array address in ar in x86_64.

I have not tested these code myself and you may be to fine tune the offsets yourself.

But I am just showing one possible hack.

3 Comments

Nice approach! I will have a look! ;-)
I have updated the code for x86. Let me also know, if the hack really works!
asm code produce segfault, but works replacing ':"=r"(*ptr)' with ':"=r"(ptr)'
1

If you want to use any function with no parameters with any return type, it should be declared as (In C)

return_type func(void). It is only generic way of function declaration.

But any how, for your question , it possible to access but not generic..Try this program...

  #include<stdio.h>
  int *p;

  void func();

  int main()
  {
    int ar[] = { 1, 2, 3 };
    p=ar;
    printf("In main %d\n",ar[0]);
    func(ar); // call with parameter
    printf("In main %d\n",ar[0]);
   return 1;
 }

 void func() // no parameters
 {
  printf("In func %d \n",*p);
    *p=20;
 }

Even this program works fine, it is not generic way and also is undefined.

if you declare function like void func (void) ,it will not work.

Comments

0

You can't access ar in func(), since you dont have a reference to it in func().

It would be possible if ar would be a global var or you have a pointer on it.

Comments

0

So that you can do something with func(), you need to pass it the input data you'll work with.

First you must declare the function properly :

// declaration
 void func(int []);

The define it :

void func( int a[] )
{
   // do something
   printf ("a[0] = %d\n", a[0]);
}

Full code :

#include <stdio.h>

// declaration
 void func(int []);

int main()
{
   int ar[] = { 1, 2, 3 };
   func(ar); // call with parameter
   return 1;
}

void func( int a[] )
{
   // do something
   printf ("a[0] = %d\n", a[0]);
}

This will display : a[0] = 1

2 Comments

This is not the question that speaks about difference between declaration and usage.
@mpromonet1 : you're right, I overlooked the question. Understood he didn't know how to use his function.
-1

You can implement something like this.

void func(int *p, int n);

int main()
{
    int ar[] = { 1, 2, 3 };
    func(ar, sizeof (ar)/sizeof(ar[0]) ); // call with parameter
    return 1;
}

void func(int *p, int n) // added 2 parameters
{
    int i=0;

    for (i=0; i<n; ++i){
        printf ("%d ", p[i]);
        }

}

1 Comment

This is not the question that speaks about difference between declaration and usage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.