LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-28-2006, 05:20 PM   #1
CM019
Member
 
Registered: Mar 2004
Posts: 58

Rep: Reputation: 15
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.
 
Old 05-28-2006, 06:20 PM   #2
Rajahuroman
Member
 
Registered: May 2004
Location: Romania; Arad
Distribution: Archlinux
Posts: 91

Rep: Reputation: 15
Thumbs up 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?

Last edited by Rajahuroman; 05-28-2006 at 06:50 PM.
 
Old 05-28-2006, 06:28 PM   #3
CM019
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
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.
 
Old 05-28-2006, 06:49 PM   #4
Rajahuroman
Member
 
Registered: May 2004
Location: Romania; Arad
Distribution: Archlinux
Posts: 91

Rep: Reputation: 15
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
 
Old 05-28-2006, 06:54 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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.
 
Old 05-28-2006, 06:57 PM   #6
CM019
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks for your help. I'm trying to learn C by myself I've a long way to go. Again thanks.
 
Old 05-28-2006, 07:01 PM   #7
Rajahuroman
Member
 
Registered: May 2004
Location: Romania; Arad
Distribution: Archlinux
Posts: 91

Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me decipher weird Apache activity paul_m_d Linux - Networking 2 07-28-2005 12:52 AM
make install ndiswrapper error I can't decipher case1984 Linux - Wireless Networking 1 09-28-2004 06:59 PM
Help decipher lines in messages file suse firewall TongueTied Linux - Security 3 05-04-2004 01:40 PM
Can anyone decipher my K3b error message? Trinity22 Linux - Software 3 04-21-2004 10:01 PM
Try to decipher this :) Whitehat General 42 06-29-2003 07:42 PM


All times are GMT -5. The time now is 01:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration