Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop drive driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory missing monitor motherboard network printer problem ram random registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Can someone explain how this works?

Reply  
Thread Tools
redivivus's Avatar
Senior Member with 1,464 posts.
 
Join Date: Mar 2006
Experience: 8th hindu reincarnation -- Praise Allah
20-Sep-2006, 05:53 PM #1
Can someone explain how this works?
Code:
#include <cstdio>
#include <iostream>

int main()
{
	char a = '\0', b = '!', c = 'd', d = 'l', e = 'r', f = 'o',
		g = 'w', h = ' ', i = ',', j = 'o', k = 'l', l = 'l', 
		m = 'e', n = 'H';

	puts(& n);
    system("PAUSE");

}
Output:

Hello, world!
Press any key to continue...


No comprendo... Looks like the string "Hello, world" is stored backwards in a char array.
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
21-Sep-2006, 12:32 PM #2
Pretty weird.

But it's not an array - it's just a list of initialized char variables; if it were an array, the program probably would have had a memory fault, as you'd expect.

I think it illustrates nothing more than the way that storage for scalar variables is allocated; last in, first out, as in a stack. It is NOT safe programming, because we as programmers shouldn't make such assumptions about memory allocations.
Shadow2531's Avatar
Senior Member with 2,640 posts.
 
Join Date: Apr 2001
21-Sep-2006, 11:31 PM #3
Quote:
Originally Posted by AGCurry
It is NOT safe programming, because we as programmers shouldn't make such assumptions about memory allocations.
Exactly. For example, the output will break if you compile with optimizations.
blaqDeaph's Avatar
Senior Member with 881 posts.
 
Join Date: Nov 2005
Location: Down Under!
Experience: Enough
22-Sep-2006, 02:32 AM #4
This only works because it assumes that the variables are assigned sequentially in the memory (stack operations assign the memory locations backwords). It won't work if for some reason, the data locations wasn't assigned sequentially.

Not to mention it's less efficient cuz the compiler/program will have to keep tabs on more variables (thus more overhead)
__________________
Windows XP: 64-bit wanna-be OS with a 32-bit graphical shell for 16-bit extensions of a 8-bit patch to an 4-bit operating system originally coded for a 2-bit microprocessor, written by a company that can't stand 1 bit of competition.
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:16 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.