Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Segmentation fault while trying to assign a value

Status
Not open for further replies.

ushtabalakh

Programmer
Jun 4, 2007
132
0
0
Hi there

I'm trying to make a module under gcc
I have a file named a1.o
and a file named a1.h
and also a file named a1.c

I'm trying to pass an array of characters from my a1.c file to a1.o , everything works perefctly but when I try to assign something to the value that I have sent from a1.o I recieve segmentation fault message.

This is the source code of a1.o
#include <stdio.h>
#include <string.h>
int encrypt(char s[],int SHIFT) {
char temp[7]; strcpy(temp, s);
printf("temp:%s s:%s\n", temp, s);
int i=0;
while(s != '\0') {
if((s<=90-SHIFT && s >=65) || (s<=122-SHIFT && s >=97)) {
printf("1-- %s %d %d %c\n", s, i, SHIFT, s);
printf("%d \n", s+SHIFT);
temp += SHIFT;
printf("2-- %s %d %d %c\n", s, i, SHIFT, s);
//return 0;
if((s<65) || (s < 97 && s > 90))
printf("3-- %s %d %d %c\n", s, i, SHIFT, s);
temp+=26;
}
else if((s <=90 && s > 90-SHIFT) || (s <=122 && s > 122-SHIFT)) {
printf("4-- %s %d %d %c\n", s, i, SHIFT, s);
temp = s-26+SHIFT;
printf("5-- %s %d %d %c\n", s, i, SHIFT, s);
if((s<65) || (s < 97 && s > 90))
printf("6-- %s %d %d %c\n", s, i, SHIFT, s);
temp+=26;
}
i++;
}
printf("temp:%s s:%s\n", temp, s);

strcpy(s, temp);
return 1;
}

/*decrypt: decrypts one line at a time using a Ceasar shift*/
int decrypt(char s[],int SHIFT) {
encrypt(s,SHIFT * -1);
return 1;
}

this is source code of a1.h
int encrypt(char s[],int SHIFT);
int decrypt(char s[],int SHIFT);

this is source code of a1.c
#include <stdio.h>
#include "cciph.h"

main() {
char* s = "ABCDEFG";
printf("%s\n", s);
if(encrypt(s, 3) == 1)
printf("%s\n", s);
//decrypt(s, 3);
//printf("%s\n", s);
}


And this is how I compile them:
gcc -c -o cciph.o cciph.c
gcc -o test.o test.c cciph.o
./test.o


I guess the problem is that the array that I'm passing to the object is read-only, how can I make it writable?
 
2 problems
1) s is actually 8 chars: one extra for the terminator so temp needs to be 8 chars.
2)It would be better if you had 2 arrays: one to be encoded and another for the result. Then you won't have to do anything illegal like writing to a readonly variable.
Code:
int encrypt(char s[],char result[], int SHIFT) {
...
   strcpy (result, temp);
   return 1;
}

int main ()
{
   char* s = "ABCDEFG";
   char result[8];

...
   encrypt (s, result, 3) == 1
}
 
hey xwb,

thanks for the reply;
but we can't return an array from the encrypt function because the function is not supposed to return a string in the given question.
 
I guess there should be a keyword that would let gcc know that a function can change the value of a given parameter in a function.

I'm looking for that keyword
 
Any parameter passed as a pointer to a variable can be changed. That means you could access s[] directly without copying it back and forth to and from another variable. Is this what you wanted to do?

Lee
 
trollacious, thanks for your reply

I can't write anything to the passed array of characters, it gives the "segmentation fraud" error whenever I try to do so, whether by assigning a character directly or by strcpy function.
 
> char* s = "ABCDEFG";
Try it with
[tt] char s[] = "ABCDEFG";[/tt]

The reason for the seg-fault is that a "string" is usually marked read-only, so any attempt to modify it (with say strcpy) immediately results in the OS killing your program.

An array on the other hand can be freely initialised and modified.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
And don't forget temp still needs to be (strlen(s) + 1) chars. It may magically crash if you provide a longer string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top