Sponsored Content
Top Forums Programming Why this C program is crashing? Post 302517289 by alister on Tuesday 26th of April 2011 10:52:55 AM
Old 04-26-2011
Quote:
Originally Posted by Perderabo
Code:
char* p="abcde";
printf("%c", 3[p]);

should work fine. I believe that this is just a test of the fact that p[3] and 3[p] are supposed to behave the same way. He probably going to enter the obfuscated C contest or something like that. Smilie
Hehehe. I was thinking the same thing.

A bit more info for those interested in this "feature": Question 6.11

And ... it gets worse:

Code:
$ cat acomm.c
#include <stdio.h>

int
main(int argc, char **argv) {
	char *s = "char *s";
	char *a[1][2][3];
	a[0][1][2] = s;

	(void) printf("%s\n", a[0][1][2]);
	(void) printf("%s\n", 2[1[0[a]]]);
	(void) printf("%s\n", 2[0[a][1]]);
	(void) printf("%s\n", 1[a[0]][2]);
	/* etc */

	return 0;
}


$ cc -Wall -pedantic acomm.c 
$ ./a.out 
char *s
char *s
char *s
char *s
$ cc --version
i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5370)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Regards,
Alister
This User Gave Thanks to alister For This Post:
 

9 More Discussions You Might Find Interesting

1. Programming

c++ class keeps crashing

i just started learning to prog in c++, im using netbeans. i tryed to make a class and i keep getting this error. what am i doing wrong ? ----------------------------- class test { public: int funtime(); private: int time; }; int test::funtime()... (4 Replies)
Discussion started by: akira300
4 Replies

2. Solaris

Solaris 10 crashing

Hi, My system is crashing with following error .. i tried to boot from the network and unencapsulated the root disk from SVM .. but still not able to boot the box , can any one point me to some direction .. i do not want to build the box new as of now just want to troubleshoot if possible.. ... (3 Replies)
Discussion started by: fugitive
3 Replies

3. Red Hat

Fedora 11 crashing help

Using Fedora 11, just about every day the system started crashing. Becomes unresponsive to keyboard/mouse, nothing appears on screen but box is still running. Still responds to ping, arp address stays alive in the firewall, other than that its unresponsive Where can I look to find out... (1 Reply)
Discussion started by: ippy98
1 Replies

4. Programming

C++ program is crashing on re-assigning const static member variable using an int pointer

Hi, Can any one tell me why my following program is crashing? #include <iostream> using namespace std; class CA { public: const static int i; }; const int CA::i = 10; int main() { int* pi = const_cast<int*>(&CA::i); *pi = 9; cout << CA::i << endl; } (6 Replies)
Discussion started by: royalibrahim
6 Replies

5. Web Development

MySQL Server Crashing need Help

Hi, we have some problem with mysql high cpu , would like some help with MySQL Tuning here are the mysqltuner & tuning-primer details mysqltuner: # mysqltuner >> MySQLTuner 1.1.1 - Major Hayden <major@mhtx.net> >> Run with '--help' for additional options and output filtering --------... (1 Reply)
Discussion started by: cataplexy
1 Replies

6. UNIX and Linux Applications

Firefox crashing

Firefox keeps on crashing every time I try to run it. I tried to create a new profile with no luck. When I try to open up the profile manager firefox crashes. I even deleted my profile folder and tried to start with a new profile. That did not work either. I don't know what the problem could be. I... (6 Replies)
Discussion started by: cokedude
6 Replies

7. UNIX for Dummies Questions & Answers

Random Crashing

Over the last month or so my CentOS server has been crashing for reasons I do not know. It has been running for over a year with regular yum updates without problems. The load on the server is perfectly normal with CPU usage at 5-6% and RAM usage at less than half of 32GB of RAM (multiple smaller... (3 Replies)
Discussion started by: spinner0205
3 Replies

8. Solaris

Solaris V440 keeps crashing

Hi, I have Sun Solaris V440, every two days or so, the system crashes and it's at OK prompt. After that I do the FSCK and clear all the bad sectors. Then system stays up for close to 48 hours. Then it crashes again. I checked the logs and I don't see anything under /var/adm/messages file. ... (1 Reply)
Discussion started by: samnyc
1 Replies

9. UNIX for Advanced & Expert Users

Application keep on crashing need to troubleshoot

Hi experts, I am using RHEL as my distro and I have a very important application running on that server. That application is a third party application and I don't have control with the source. That application keeps on crashing or stop like every other day and it doesn't have any traces on... (1 Reply)
Discussion started by: cwiggler
1 Replies
XPRINTF(3)						   BSD Library Functions Manual 						XPRINTF(3)

NAME
asxprintf, dxprintf, fxprintf, sxprintf, xprintf, vasxprintf, vdxprintf, vfxprintf, vsxprintf, vxprintf -- extensible printf SYNOPSIS
#include <printf.h> int asxprintf(char ** restrict ret, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int dxprintf(int fd, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int fxprintf(FILE * restrict stream, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int sxprintf(char * restrict str, size_t size, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int xprintf(printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); #include <stdarg.h> int vasxprintf(char ** restrict ret, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vdxprintf(int fd, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vfxprintf(FILE * restrict stream, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vsxprintf(char * restrict str, size_t size, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vxprintf(printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); DESCRIPTION
These extensible printf (see xprintf(5)) variants behave like their normal printf counterparts (see printf(3)) without 'x' in the name (except sxprintf() and vsxprintf() behave like snprintf() and vsnprintf(), respectively). The domain argument must be a pointer to a printf domain structure, as returned by one of the functions described in xprintf_domain(3). The loc argument should be an extended locale (see xlocale(3)) or NULL, which means to use the current locale in effect (either the per-thread locale if set, or the global locale by default). SEE ALSO
printf(3), xlocale(3), xprintf_domain(3), xprintf(5) Darwin Aug 19, 2012 Darwin
All times are GMT -4. The time now is 06:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy