LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2005, 03:15 AM   #1
ar1
Member
 
Registered: Nov 2003
Location: Bloomington, IL, USA
Distribution: Fedora Core 3
Posts: 126

Rep: Reputation: 15
C Code problem


I wrote a program to give the permutations of a string, and get a segmentation fault in the function "swap".

Now, the program runs fine if I declare the string as "char str[10] = "ABCD" rather than as char *str = "ABCD".

Can anyone tell me why the segmentation fault occurs in the first case? I tried using a debugger but everything seemed to be the same in both cases, yet the error :/

Thanks
Code:
/*
 *	perms.c : List the permutations of a string P(n,c)
 */


int N = 4;
int C = 4;

void perms(int, char *);
void swap(char *, char *);

int main()
{
	char *str = "ABCD";
	perms(0, str);
	return 0;
}

void perms(int i, char *str)
{
	int j;
	
	if (N == i)
		printf("%s\n", str);
	else
	{
		for (j = i; j < N; j++)
		{
			swap(str + i, str + j);
			perms(i+1, str);
			swap(str + i, str + j);
		}
	}
}

void swap(char *s, char *t)
{
	char tmp;
	tmp = *s;
	*s = *t;
	*t = tmp;
}
 
Old 05-09-2005, 04:55 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
When you write a string literal, i.e.:
Code:
"hello"
that string lives in static storage and is of type
Code:
const char *
So the string in your code is of really of type const char * and not of type char *. A decent compiler should
issue a warning if try to declare a string literal to have the type char *.
If you're using gcc, I suggest always compiling with -Wall -W, and sometimes -ansi and -pedantic may also be warranted.
You already have the solution to your problem, you want a string to be initialised and modifiable and the correct way
is to use an array:
Code:
char foo[] = "hello";
The compiler will fill in the size for you (six characters in this case).
Trying to modify string literals is a common mistake.
 
Old 05-09-2005, 06:22 AM   #3
ar1
Member
 
Registered: Nov 2003
Location: Bloomington, IL, USA
Distribution: Fedora Core 3
Posts: 126

Original Poster
Rep: Reputation: 15
Hivemind, thanks for your reply. Actually, gcc is not giving the error even with -Wall -W, nor with -ansi and -pedantic.

What do you mean by "static storage"? Do you mean the stack? If so, then all variables initialized in the program are also stored there but can be modified :/

Plus, it seems to me that even if
Code:
char foo[] = "hello"
is dynamic,
Code:
 char foo[10] = "hello"
should not be dynamic, yet it doesn't give an error in my program.

Can you explain further? thanks.
 
Old 05-09-2005, 07:04 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
I already did explain it. If a variable has static storage it means it exists throughout the lifetime of the program, and string literals have static linkage and are not modifiable. Programs living on the stack or heap do not automatically exist through the lifetime of a program and whether they can be modified are usually controllable by the programmer (by declaring them const or not).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
what the... C code problem zaichik Programming 6 01-26-2005 02:08 PM
What is the problem with the below C++ code? coolguy_iiit Programming 4 01-24-2005 12:28 PM
Problem with Code eggoz Linux - General 6 04-09-2003 09:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration