I solved the codewars training as below. I'm getting an error message, what's wrong in the code? I don't know what I made a mistake, so please let me know.
Test Crashed Caught unexpected signal: SIGSEGV (11). Invalid memory access.
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char *work_on_strings(const char *a, const char *b) {
size_t a_size = strlen(a), b_size = strlen(b), count;
char *a2 = a, *b2 = b,
*str = (char*)calloc(a_size + b_size + 1,sizeof(char)),
*base = str;
*a2 = a;
while(*a2)//文字列aから
{
count = 0;
*b2 = b;
while(*b2){if(*a2 == *b2++)count++;} //bにいくつ同じ文字があるか
if(count % 2 == 0)*str++ = *a2; //同じ文字の数が0もしくは2の倍数だったら
else //同じ文字の数が奇数だったら
{
if(*a2 >= 'A' && *a2 <= 'Z')*str++ = tolower(*a2); //大文字は小文字に
else if(*a2 >= 'a' && *a2 <= 'z')*str++ = toupper(*a2); //小文字は大文字にして格納
else ;
}
a2++;
}
*b2 = b;
while(*b2) //文字列bも同様に
{
count = 0;
*a2 = a;
while(*a2){if(*b2 == *a2++)count++;}
if(count % 2 == 0)*str++ = *b2;
else
{
if(*b2 >= 'A' && *b2 <= 'Z')*str++ = tolower(*b2);
else if(*b2 >= 'a' && *b2 <= 'z')*str++ = toupper(*b2);
else ;
}
b2++;
}
return base;
}
It also gives a warning as it may be a hint. I'm not sure about the warning about "const" either. warning
solution.c:6:7: warning: initializing 'char *' with an expression of type 'const char *' discards
qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
char *a2 = a, *b2 = b,
^ ~
solution.c:6:16: warning: initializing 'char *' with an expression of type 'const char *' discards
qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
char *a2 = a, *b2 = b,
^ ~
solution.c:10:5: warning: incompatible pointer to integer conversion assigning to 'char' from 'const
char *' [-Wint-conversion]
*a2 = a;
^ ~
solution.c:14:7: warning: incompatible pointer to integer conversion assigning to 'char' from 'const
char *' [-Wint-conversion]
*b2 = b;
^ ~
solution.c:26:5: warning: incompatible pointer to integer conversion assigning to 'char' from 'const
char *' [-Wint-conversion]
*b2 = b;
^ ~
solution.c:30:7: warning: incompatible pointer to integer conversion assigning to 'char' from 'const
char *' [-Wint-conversion]
*a2 = a;
^ ~
6 warnings generated.
The idea behind using const is that you don't want to change the data stored at that memory address. It's called an immutable character/string.
So if you do this:
const char* a = "This is a string";
char* a2 = something;
Now both a and a2 point to the same area in the memory, and that area/region of memory doesn't allow changes, so every time you try to change it you will get an error.
Possible solution: you can try and copy the content into a different region in memory.
#include<stdio.h>
#include<string.h>
int main()
{
const char *a = "This is a string";
char *a2 = malloc(sizeof(a)/sizeof(char));
strcpy(a2, a); // copy data from a to a2
printf("First string (a) = %s\n", a);
printf("Second string (a2) = %s\n", a2);
// signal to operating system program ran fine
return 0;
}
Output:
First string (a) = This is a string
Second string (a2) = This is a string
Here a and a2 are 2 variables which have the same data, both point to a region in memory which holds the characters "This is a string", but those region are different from each other, on region allows changes while the other doesn't.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.