473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

array a[(foo)()] (help)

int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}
int main(void)
{
unsigned int a[(foo)()];
printf("%d\n",sizeof a);
return 0;
}
gcc does not give any compile time error. But the program crashes. why?
Array 'a' can not
be decleared with size 10 * 4 size? Please help.

Nov 14 '05 #1
8 1288

"noname_is_me" <no****@mailno.com> wrote in message news:2q*************@uni-berlin.de...
int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}
int main(void)
{ bar();
{ unsigned int a[(foo)()];
printf("%d\n",sizeof a); } return 0;
}
gcc does not give any compile time error. But the program crashes. why?
Array 'a' can not
be decleared with size 10 * 4 size? Please help.


--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Nov 14 '05 #2
noname_is_me <no****@mailno.com> wrote:
int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
There's no need for the address operator here.
return a;
}
int main(void)
{
unsigned int a[(foo)()];
You don't need the parentheses around 'foo'.
printf("%d\n",sizeof a);
return 0;
}
gcc does not give any compile time error. But the program crashes. why?


If you run gcc in c90-compliant mode it's going to warn you about
variable-size array you're using, which are only allowed in C99. And
another problem is that in the calculation of the size you use the
uninitialized pointer 'foo' as if it would point to a function,
which it doesn't ('foo' would only gets set in function bar(), but
that is never called).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
noname_is_me wrote:
int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}
int main(void)
{ bar(); /* added */ unsigned int a[(foo)()];
printf("%d\n",sizeof a);
return 0;
}
gcc does not give any compile time error. But the program crashes. why?
Array 'a' can not
be decleared with size 10 * 4 size? Please help.


I am a beginner in C so i am not sure if i am right or wrong.
When the main() is called,at that time foo doesnt have the address of
bar(),so obviously it will try to go to some garbage value contained in
foo and try to execute it and thus results in a crash.
As for gcc,execute bar function before your declaration.

As ANSI C doesn't allow "variable-size array".Moreover the solution here
is also not ANSI C complaint.It gives error: ISO C89 forbids mixed
declarations and code.

Nov 14 '05 #4
noname_is_me <no****@mailno.com> wrote:
int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}

int main(void)
{
unsigned int a[(foo)()];
printf("%d\n",sizeof a);
return 0;
}
gcc does not give any compile time error. But the program crashes. why?
Array 'a' can not be decleared with size 10 * 4 size?


Array a has nothing to do with it. You're calling an uninitialised
function pointer; foo is never given a value.

Richard
Nov 14 '05 #5


Alex Vinokur wrote:
"noname_is_me" <no****@mailno.com> wrote in message news:2q*************@uni-berlin.de...

int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}
int main(void)
{

bar();
{

I must call the function bar() since it initializes the function pointer
foo. Am I correct?

unsigned int a[(foo)()];
printf("%d\n",sizeof a);

}

return 0;
}
gcc does not give any compile time error. But the program crashes. why?
Array 'a' can not
be decleared with size 10 * 4 size? Please help.



Nov 14 '05 #6
Amit <am**********@oracle.com> wrote:
noname_is_me wrote:
int main(void)
{ bar(); /* added */
unsigned int a[(foo)()];
printf("%d\n",sizeof a);
return 0;

When the main() is called,at that time foo doesnt have the address of
bar(),so obviously it will try to go to some garbage value contained in
foo and try to execute it and thus results in a crash.
This is right.
As ANSI C doesn't allow "variable-size array".Moreover the solution here
is also not ANSI C complaint.It gives error: ISO C89 forbids mixed
declarations and code.


This is wrong. ANSI C, these days, is ISO C99; at least, I'd be very
surprised if ANSI had not ratified C99. ANSI C89 and ISO C90 did not
allow the things you mention, that's true.

Richard
Nov 14 '05 #7

"noname_is_me" <no****@mailno.com> wrote in message news:2q*************@uni-berlin.de...


Alex Vinokur wrote:
"noname_is_me" <no****@mailno.com> wrote in message news:2q*************@uni-berlin.de...

int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}
int main(void)
{

bar();
{

I must call the function bar() since it initializes the function pointer
foo. Am I correct?


Yes, you are correct.

unsigned int a[(foo)()];
printf("%d\n",sizeof a);

}

return 0;
}
gcc does not give any compile time error. But the program crashes. why?
Array 'a' can not
be decleared with size 10 * 4 size? Please help.


--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Nov 14 '05 #8

Thanks for all the quick responses

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sims | last post by:
Hi I have a table with something like ID PARENT 0 | -1 1 | -1 2 | 1 3 | 1
4
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
5
by: Eclectic | last post by:
Hey all, I have been using usort to sort my multi dimensional arrays ... function cmp($a, $b){ if($a == $b){ return 0; } return ($a < $b) ? -1 : 1; }
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
2
by: John Machin | last post by:
Googling for "pickle array" in comp.lang.python yields old messages that show a PickleError -- plus one message where Alex Martelli writes "I am but an egg" :O) Looks like arrays are NOW (2.4.1)...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
4
by: Mike Windsor | last post by:
This seems like a fairly basic thing to want to to, but as far as I can tell, it's not possible. Can anyone help? I have a class 'Foo' and I want to call the constructor with a particular...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
3
by: Spoon | last post by:
Hello everyone, I want to create an array of objects at run-time. AFAIU, operator new will call the default constructor for each object in the array. In other words, the following program will...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.