"Error reading characters of string" C, Visual Studio 2013

1

Ive been asked to write a function that merges two sorted (ascii sorted) strings without duplicates. For example, for string1 = aabcd, and string2 = abbcdg, the end result string should be abcdg. For some reason, the end result string doesnt allocate well, or so I think.. its not working anyway and its giving me weird characters instead of what its supposed to give. The value of stringToReturn is always 0xfffffffe "Error reading characters of string", and inside it says "Unable to read memory"

main.c:

#include <stdio.h>
#include <stdlib.h>
#include "bohan.h"

int main() {
char* string1;
char* string2;
char* mergedString;
string1 = (char*)malloc(MAX_TEXT + 1);
if (string1 == NULL)
    return;
string2 = (char*)malloc(MAX_TEXT + 1);
if (string2 == NULL)
    return;
printf("Please enter string no. 1: ");
scanf("%s", string1);
printf("Please enter string no. 2: ");
scanf("%s", string2);
mergedString = merge_strings(string1, string2);
printf("%s \n", mergedString);

free(string1);
free(string2);
free(mergedString);
}

bohan.c:

#include <stdio.h>
#include <stdlib.h>
#include "bohan.h"

int checkNumberOfChars(char* text) {
int sum = 0;
if (text == NULL)
    return 0;
while (*text != '\0')
{
    sum++;
    text++;
}
return sum;
}

char* merge_strings(char* text1, char* text2) {
int i;
int hasChanged;
char* stringToReturn;
if (text1 == NULL && text2 == NULL)
    return NULL;
stringToReturn = (char *)malloc(checkNumberOfChars(text1) + checkNumberOfChars(text2) + 1);
if (stringToReturn == NULL)
    return NULL;
for (i = 1; i <= MAX_ASCII; i++) {
    hasChanged = FALSE;
    if (*text1 != '\0' || *text2 != '\0') {
        if (*text1 != '\0') {
            if (i == *text1) {
                *stringToReturn = i;
                stringToReturn++;
                hasChanged = TRUE;
                while (*text1 == i)
                    text1++;
            }
        }
        if (*text2 != '\0') {
            if (i == *text2) {
                if (!hasChanged) {
                *stringToReturn = i;
                stringToReturn++;
                }
                while (*text2 == i)
                    text2++;
            }
        }
    }
    else
        break;
}
return stringToReturn;
}

bohan.h:

#ifndef DEF
#define TRUE 1
#define FALSE 0
#define MAX_TEXT 100
#define MAX_ASCII 255

int checkNumberOfChars(char *text);
char *merge_strings(char *text1, char *text2);

#endif DEF
c
string

1 Answer

2

In the merge_strings method you must define a pointer (beginOfStringToReturn) to hold the address of the beginning of the merged string. The merge_strings method should return this pointer at the end. Also add '\0' after the merged string has been built.

char* merge_strings(char* text1, char* text2) {
int i;
int hasChanged;
char* stringToReturn;
if (text1 == NULL && text2 == NULL)
    return NULL;
stringToReturn = (char *)malloc(checkNumberOfChars(text1) + checkNumberOfChars(text2) + 1);
char* beginOfStringToReturn = stringToReturn;
if (stringToReturn == NULL)
    return NULL;
for (i = 1; i <= MAX_ASCII; i++) {
    hasChanged = FALSE;
if (*text1 != '\0' || *text2 != '\0') {
    if (*text1 != '\0') {
        if (i == *text1) {
            *stringToReturn = i;
            stringToReturn++;
            hasChanged = TRUE;
            while (*text1 == i)
                text1++;
        }
    }
    if (*text2 != '\0') {
        if (i == *text2) {
            if (!hasChanged) {
            *stringToReturn = i;
            stringToReturn++;
            }
            while (*text2 == i)
                text2++;
        }
    }
}
else
    break;
}
*stringToReturn = '\0';
return beginOfStringToReturn;
}

In bohan.h include the guard like this:

#ifndef BOHAN_H
#define BOHAN_H
#define TRUE 1
#define FALSE 0
#define MAX_TEXT 100
#define MAX_ASCII 255

int checkNumberOfChars(char *text);
char *merge_strings(char *text1, char *text2);

#endif
answered on Stack Overflow Dec 2, 2015 by Peter • edited Dec 3, 2015 by Peter

User contributions licensed under CC BY-SA 3.0