C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-04-2009, 10:41 AM   #16
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by WatchTower View Post
You need return 0 in your main function.
It's optional according to latest C standards.
Quote:
Originally Posted by Adak View Post
Perhaps it doesn't run on your compiler, but it runs just fine on mine (Turbo C).
Might be because it's an old compiler.
__________________
HOPE YOU UNDERSTAND.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 07-04-2009, 10:57 AM   #17
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,359
Quote:
Originally Posted by Adak
Perhaps it doesn't run on your compiler, but it runs just fine on mine (Turbo C).
I am curious to know just what you mean by "run on your compiler". Your code compiles on the MinGW port of gcc 3.4.5 with just one warning (you forgot to #include <string.h>), but it is logically incorrect since it attempts to modify a string literal, as that part of the OP's code was not fixed. My guess is that you (incorrectly) interpreted "even the code you gave is not running" as "even the code you gave does not compile". If not, and you actually ran the program and it worked, then it is just an effect of undefined behaviour - that it compiles is no guarantee of correctness.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-04-2009, 08:44 PM   #18
Registered User
 
Join Date: Sep 2006
Posts: 2,511
Quote:
Originally Posted by laserlight View Post
I am curious to know just what you mean by "run on your compiler". Your code compiles on the MinGW port of gcc 3.4.5 with just one warning (you forgot to #include <string.h>), but it is logically incorrect since it attempts to modify a string literal, as that part of the OP's code was not fixed. My guess is that you (incorrectly) interpreted "even the code you gave is not running" as "even the code you gave does not compile". If not, and you actually ran the program and it worked, then it is just an effect of undefined behaviour - that it compiles is no guarantee of correctness.

I mean the compiler is able to run through it's work, and create an executable file, without warnings or errors. Turbo C has a smart compiler which is able to include strlen() functionality, into a program, despite not explicitly including string.h.

The resulting program runs correctly. Some sample output appropriate for July 4th:

Quote:


Hoorah for the flag of the free!
May it wave as our standard forever
The gem of the land and the sea
The banner of the right.
Let tyrants remember the day
when our fathers with mighty endeavor,
proclaimed as they marched to the quay
that by their might and by their right
it waves forever...


!eerf eht fo galf eht rof harooH
reverof dradnats ruo sa evaw ti yaM
aes eht dna dnal eht fo meg ehT
.thgir eht fo rennab ehT
yad eht rebmemer stnaryt teL
,rovaedne ythgim htiw srehtaf ruo nehw
yauq eht ot dehcram yeht sa demialcorp
thgir rieht yb dna thgim rieht yb taht
...reverof sevaw ti


Press Enter When Ready
Adak is offline   Reply With Quote
Old 07-04-2009, 11:50 PM   #19
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by laserlight View Post
I am curious to know just what you mean by "run on your compiler". Your code compiles on the MinGW port of gcc 3.4.5 with just one warning (you forgot to #include <string.h>), but it is logically incorrect since it attempts to modify a string literal, as that part of the OP's code was not fixed. My guess is that you (incorrectly) interpreted "even the code you gave is not running" as "even the code you gave does not compile". If not, and you actually ran the program and it worked, then it is just an effect of undefined behaviour - that it compiles is no guarantee of correctness.
Now after searching the net I get to know that string literals can't be modified as they ar stored in read-only section of the memory by the OS. But my question is why string literals are given such type of a special "treatment"?
And by the way is anything between double quotes a string literal?

Quote:
Originally Posted by Adak View Post
I mean the compiler is able to run through it's work, and create an executable file, without warnings or errors. Turbo C has a smart compiler which is able to include strlen() functionality, into a program, despite not explicitly including string.h.

The resulting program runs correctly. Some sample output appropriate for July 4th:
Maybe the last line of this FAQ is helpful here.
C-FAQ
__________________
HOPE YOU UNDERSTAND.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 07-05-2009, 01:50 AM   #20
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by roaan View Post
Hi,

I have a doubt about string reversal. The code below reverses the string character by character but when i give it to print out the result there occurs an error. I am unable to figure out where am i going wrong.

insert
Code:
#include <stdio.h>
#include <stdlib.h>


void xstrrev(char *);

int main(){

	int i;
	char *s[] = {
		"To err is human...",
		"But to really mess things up...",
		"One needs to know C!!"
	};

	for(i=0;i<3;i++){
		xstrrev(s[i]);
	}
}


void xstrrev(char *a){


	int len,j;
	char *p;

	printf("\n\r%s", a);
	len = strlen(a);
	printf("\n\r%d", len);
	p = malloc(len+1);

	for(j = 0;j<=len;j++){	
		p[j] = a[len-j];
		printf("\n\r%c%c", a[len-j], p[j]);
		if(j == len)
			printf("\n\r%s", p);
	} }
After going through the details of arrays of pointers and character strings(and also hijacking your thread) I made 3 solutions of this question. But in all of them the main thing is that I'm swapping the characters of each string until the mid-character. Out of the three approaches I took one of them was with malloc which is similar to your code posted. But I dont understand what you're trying to do in the colored lines. What I've done is after assigning the storage to p, copied a into p and then the same swapping thing. This way the swapped string gets stored in p.
__________________
HOPE YOU UNDERSTAND.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 07-05-2009, 05:57 AM   #21
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,359
Quote:
Originally Posted by Adak
I mean the compiler is able to run through it's work, and create an executable file, without warnings or errors.
That is, the compiler was able to compile the code without warnings or errors. That is different from saying that the program does not run (but admittedly I see the potential for confusion since a program that does not compile obviously does not run: perhaps BEN10 should have mentioned a run time error or something).

Quote:
Originally Posted by Adak
Turbo C has a smart compiler which is able to include strlen() functionality, into a program, despite not explicitly including string.h.
Haha. That is not the sign of a "smart compiler", since a compiler (or linker/loader invoked by the compiler) might link to the C standard library by default, and it is possible for a function to be used without being declared at the point of usage. One possible reason that you did not get a warning is that <string.h> could have been included by a header that you did include - relying on this would be relying on something implementation specific. Another possible reason is that the warning level was not high enough.

Quote:
Originally Posted by Adak
The resulting program runs correctly. Some sample output appropriate for July 4th:
As such, you are observing the effect of undefined behaviour. It appears to work in your case, but is nonetheless incorrect.

Quote:
Originally Posted by BEN10
But my question is why string literals are given such type of a special "treatment"?
I believe there are practical reasons related to how implementations might implement string literals, but consider this: what does it mean to modify a literal? For example, does this make sense to you?
Code:
1 = 2;
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-05-2009, 09:16 AM   #22
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Thanks everybody. Now I think some of the loops in my mind are opening regarding pointers.
__________________
HOPE YOU UNDERSTAND.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
String issues The_professor C++ Programming 7 06-12-2007 09:11 AM
Custom String class gives problem with another prog. I BLcK I C++ Programming 1 12-18-2006 03:40 AM
Classes inheretance problem... NANO C++ Programming 12 12-09-2002 03:23 PM
creating class, and linking files JCK C++ Programming 12 12-08-2002 02:45 PM
Warnings, warnings, warnings? spentdome C Programming 25 05-27-2002 06:49 PM


All times are GMT -6. The time now is 04:33 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22