LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to decipher this C declaration? (https://www.linuxquestions.org/questions/programming-9/how-to-decipher-this-c-declaration-449404/)

CM019 05-28-2006 04:20 PM

How to decipher this C declaration?
 
If you look at the declaration below for Gnu C:

Quote:

int (*x(int))[5];
So i've created a program like this:

Code:

#include <stdio.h>

int (*x(int))[5];
int (*p)[5];

int aaa[][5] = {{0, 0, 0, 0, 0}};

*****  x(int i) //Don't know what to use as type?
{
   
    p = aaa;
    printf("Was here!!!!!!!!!\n");
    return (*p)[5];
}

int main()
{
 
  x(2);
 
  return 0;
}

What type will i use for function x? Is it possible to define and call x? Also if i use the top declaration all by itself in a gnu c file it allows it to compile? If not possible why is it being allowed to be declared like this? Any ideas?


Thanks.

Rajahuroman 05-28-2006 05:20 PM

Problem solved:
 
Code:

#include <stdio.h>

int (*x(int))[5];
int (*p)[5];

int aaa[][5] = {{0, 0, 0, 0, 0}};

int (*x(int i))[5] //This is the type to use
{
   
    p = aaa;
    printf("Was here!!!!!!!!!\n");
    return (*p)[5];
}

int main()
{
 
  x(2);
 
  return 0;
}


Very interesting program, how ever did you stumble across it?
I did'nt even now you could declare variables like that, It represents the type of int (*p)[5] which should be 5 values of evaluated int* addresses. C is really something isn't it?

CM019 05-28-2006 05:28 PM

I found it in a exercise of a book(C Programming:A modern approach). I can't find any answer to this problem... I guess I'm still a newbie. And yes, I'm learning new things in C everyday.

Rajahuroman 05-28-2006 05:49 PM

Quote:

Originally Posted by CM019
I can't find any answer to this problem...

I don't think I've bean to clear on this:

The code I posetd is the answer. The type you should use is:
int (*x(int i))[5]

Nice book. Try Kernigan and Richie. That's what we study at the Computer Science Faculty

aluser 05-28-2006 05:54 PM

Rajahuroman's version gives an error under -Wall because the return statement in x returns an int while x is, I think, declared to return a pointer to array of 5 ints. Here's one that compiles without warnings:
Code:

#include <stdio.h>

int foo[] = { 1, 2, 3, 4, 5 };

int (*x(int))[5];

int (*x(int a))[5]
{
        return &foo;
}

int main()
{
        int i;
        int (*p)[5];
        p = x(3);
        for (i = 0; i < 5; ++i)
                printf("%d\n", (*p)[i]);
        return 0;
}

changing "return (*p)[5]" to "return p" seems to work remove the warning from Rajahuroman's.

In short, x() is declared to be a function which takes an integer argument and returns a pointer to int, with the information that there are 5 integers starting at the spot to which the pointer points. Because the compiler sees *p as an array and not just an integer, (*p) can be indexed with [].

At least, that's what I guess after playing with a debugger on it. it's definitely some obscure C.

CM019 05-28-2006 05:57 PM

Thanks for your help. I'm trying to learn C by myself I've a long way to go. Again thanks.

Rajahuroman 05-28-2006 06:01 PM

Quote:

Originally Posted by aluser
Rajahuroman's version gives an error under -Wall because the return statement in x returns an int while x is, I think, declared to return a pointer to array of 5 ints.

if it's under -Wall it's a WARNING not an error ;) . Sorry, I ussually use -Wall but this time I concetrated on answering the "what type should I use?" question and after I found the answer, I didn't check the rest of the code. When I ran the programm I got the string inside that function displayed. aluser is right though. You should allways try to eliminate all warnings from you code.


All times are GMT -5. The time now is 09:40 AM.