-1

so i know the bases of programming, i have a decent amount of experience with java, but im learning C for school right now. I still dont completely understand the whole pointer aspect, which is what im sure caused the fault. This program works fine when run on my computer, but when i try and run it on my schools unix shell it gives me a seg fault. if someone could please explain to me why or how ive misused hte pointers, that would help me greatly.

//Matthew Gerton
//CS 222 - 002
//10/10/14
//HW Six

//libraries
#include<stdio.h>
#include<string.h>
#define max_Length 256

//prototypes
void decode(char *a, char *b);
void trimWhite(char *a);
void encode(char *a, char *b);

int main(void)
{
    //character arrays
    char coded[max_Length], decoded[max_Length];

    //decode the sample phrase
    char sample[] = {'P','H','H','W','D','W','C','R','R','F','D','Q','F','H','O','H','G','J',
                'R','W','R','P','H','W','U','R','K','R','W','H','O','U','R','R','P','I','R','X','U'};

    decode(sample, decoded);

    //scans a user input string to decode, and decodes it
    printf("\nPlease enter a phrase to decode: ");
    gets(coded);
    trimWhite(coded);
    decode(coded, decoded);

    //scans a user input phrase to encode
    printf("\nPlease enter a phrase to encode: ");
    gets(coded);
    trimWhite(coded);
    encode(coded, decoded);
}

//removes any spaces from the input
void trimWhite(char *a)
{
    char temp[max_Length];
    int  z=0, y=0;
    while(a[z]!='\0')
    {
        if(a[z]!=' ')
        {
           temp[y]=a[z];
           y++;
        }
        z++;
    }
    temp[y] = '\0';
    strcpy(a,temp);
  }

//decodes any phrase
void decode(char *a, char *b)
{
    int i=0,n;
    memset(b, '\0', sizeof(b));
    while(a[i]!='\0')
    {
        n=(int)a[i];
        if(n<97)
            n=n+32;
        if(n<=99)
            n=n+23;
        else
            n = n-3;
        b[i]= (char) n;
        i++;
    }
    b[i]='\0';
    printf("Coded message: %s\n", a);
    printf("Decoded message: %s\n", b);
}
//codes an input phrase
void encode(char *a, char *b)
{
    int i=0,n;
    memset(b, '\0', sizeof(b));
    strcpy(b,a);
    while(a[i]!='\0')
    {
        n=(int)a[i];
        if(n<97)
            a[i] = (char)(n+32);
        if((n>120)
            a[i] = (char)(n-23);
        else
            a[i] = (char)((n+3);

        i++;
    }
    printf("Coded message: %s\n", a);
}
matthew g
  • 11
  • 5

1 Answers1

0
  • Your main problem is here:

    char sample[] = {'P','H','H', /* snip */ ,'R','X','U'};
    

    The sample[] array is not zero-terminated which may cause the decode() function to copy many more characters than intended, thus overwriting other variables. You need to explicitly add a terminating zero when using an initializer-list:

    char sample[] = {'P','H','H', /* ... */ ,'R','X','U',0};

    Or you can initialize the array using a string literal, which does include a terminating zero:

    char sample[] = "PHHWDWCRRFDQFHOHGJRWRPHWURKRWHOURRPIRXU";
    
  • You should probably read "Why is the gets function dangerous".

  • ...

    void decode(char *a, char *b)
    {
        int i=0,n;
        memset(b, '\0', sizeof(b));
    

    Also note that the size of the array is lost when it is passed to a function. The function only receives a pointer to its first element. The memset() call above will only zero sizeof(char*) bytes (usually 4 or 8). This doesn't matter though because, as far as I can tell, you only need to zero the first byte. You could simply write:

        b[0] = 0;
    
Community
  • 1
  • 1
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42