ushtabalakh
Programmer
- Jun 4, 2007
- 132
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?
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?