473,405 Members | 2,287 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,405 software developers and data experts.

return statement

Can anyone explain me the output of the following code

char *rev(int val);
void main()
{
extern char dec[];
printf("%c", *rev);
}
char *rev(int val)
{
char dec[]="abcde";
return dec;
}
Jul 24 '06 #1
1 2423
Banfa
9,065 Expert Mod 8TB
This code doesn't compile without warnings (assuming you are using a C file, in C++ is just doesn't compile). Unless you have a very good reason (like a legacy project with 100s of files) then you should always eliminate all warning messages by correcting the code.

However it does compile but since you have written main incorrectly you have invoked undefined behaviour, under undefined behaviour the program is not required to do(or not do) anything so it could output the entire works of Shakespear or it could output nothing. In my case it output a club (from cards) symbol.

Here is your code with my comments in bold

Expand|Select|Wrap|Line Numbers
  1. /* No headers are included, stdio.h is required for printf 
  2.    and stdlib.h is often a good idea too */
  3.  
  4. char *rev(int val);
  5. void main()
  6. /* Main is declared incorrectly it should be
  7. int main(int argc, char **argp)
  8.  */
  9. {
  10.   extern char dec[];
  11. /* You have tried to declare a external reference to a 
  12.    non-existent external variable.  dec in the function below 
  13.    is local to the function and not available in this scope. */
  14.   printf("%c", *rev);
  15. /* I assume this is meant to call the function rev but the 
  16.    syntax is wrong, it should be something like
  17.   printf("%c", *rev(0));
  18. */
  19. }
  20.  
  21. char *rev(int val)
  22. /* variable val not used */
  23.   char dec[]="abcde";
  24.   return dec;
  25. /* You are returning a reference to a local variable, dec.  dec will no longer exist once the function has returned because it is an automatic variable (often placed on the stack) so the call function will be referencing a memory location that is not allocated resulting in undefined behaviour.  Declare dec outside the function or static.*/
  26. }
  27.  
Jul 24 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
3
by: Test | last post by:
Sorry, some people may have asked this question before. It is really hard to find relevant articles about this topic on the web using key words. I know it is not recommended to use return...
4
by: bluedolphin | last post by:
This is my first function in Visual and I'm having a simple syntax syntax issue that I'm hoping someone can help correct for me. I have a function Public Function...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
3
by: Marlene Stebbins | last post by:
My program has code like this: bigint bigSub(bigint minuend, bigint subtrahend) { bigint bigdiff; bigInit(&bigdiff); if(((cmp_ops(minuend.number, subtrahend.number))==1) && minuend.sign...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
by: jimjim | last post by:
Hello all, I ve come across the following code fragment and I was wondering why is the copy ctr called on return (rather than just returning the string statement obj. TIA string...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.