This question already has answers here:
EXC_BAD_ACCESS when using strcpy()?
(3 answers)
Why can I not modify a string literal in c?
(1 answer)
Closed 2 years ago.
Why my code returns:
Exception: EXC_BAD_ACCESS (code=2, address=0x10d637fa2)
in the first time it enters the for loops?
void unAnnoyWord(char *str) {
char *out = str, mostAnnoying = 'a';
do
{
if (*str != mostAnnoying)
{
*out = *str;
}
} while (*str++);
}
char *str = "hakuna matata";
unAnnoyWord(str);
Perhaps this is what you want:
#include <stdio.h>
void unAnnoyWord(char *str) {
char *out = str, mostAnnoying = 'a';
do {
if (*str != mostAnnoying) {
*out++ = *str;
}
} while (*str++);
}
void main() {
char str[] = "hakuna matata";
char *sstr = str;
printf("%s\n",sstr);
unAnnoyWord(str);
printf("%s\n",sstr);
}
Resulting in:
hakuna matata
hkun mtt
Related
This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Returning an array using C
(8 answers)
Closed 3 years ago.
I have implemented my own strchr() function in C.
When calling the function and saving the output into a pointer I go ahead and print the stream of chars and the result looks fine. However, after calling a printf(), my stream of chars gets cut off for some reason. Does anyone understand why?
Here is my code:
#include <stdio.h>
#include <string.h>
char *mon_strchr(const char *chaine, int car)
{
int j = 0, i;
char *pbuff, buff[256];
pbuff = buff;
for (i = 0; chaine[i] != '\0'; i++)
{
if ((int)chaine[i] == car)
break;
i++;
}
for (i; chaine[i] != '\0'; i++)
{
buff[j] = chaine[i];
j++;
}
return pbuff;
}
int main()
{
const char str[] = "http://www.tutorialspoint.com";
const char ch = '.';
char *ret;
ret = mon_strchr(str, ch);
printf("%s\n", ret);
printf("String after |%c| is\n", ch);
printf("%s\n", ret);
return (0);
}
And this is the output:
.tutorialspoint.com
String after |.| is
.tutoria
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Definitive List of Common Reasons for Segmentation Faults
(1 answer)
Closed 5 years ago.
I have the following code to split strings by tokens:
char **strToWordArray(char *str, const char *delimiter)
{
char **words;
int nwords = 1;
words = malloc(sizeof(*words) * (nwords + 1));
int w = 0;
int len = strlen(delimiter);
words[w++] = str;
while (*str)
{
if (strncmp(str, delimiter, len) == 0)
{
for (int i = 0; i < len; i++)
{
*(str++) = 0;
}
if (*str != 0) {
nwords++;
char **tmp = realloc(words, sizeof(*words) * (nwords + 1));
words = tmp;
words[w++] = str;
} else {
str--;
}
}
str++;
}
words[w] = NULL;
return words;
}
If I do this:
char str[] = "abc/def/foo/bar";
char **words=strToWordArray(str,"/");
then the program works just fine but if I do this:
char *str = "abc/def/foo/bar";
char **words=strToWordArray(str,"/");
then I get a segmentation fault.
Why is that? The program expects a char* as an argument then why does a char* argument crash the program?
Because the function contains:
*(str++) = 0;
which modifies the string that was passed to it. When you do:
char *str = "abc/def/foo/bar";
str points to a read-only string literal. See the section titled Attempting to modify a string literal in this question:
Definitive List of Common Reasons for Segmentation Faults
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 7 years ago.
code1:
int main()
{
char tmp[20] = "1.04";
printf("float str is %s\n",tmp);
delcharPoint(tmp);
printf("%s\n",tmp);
}
void delcharPoint(char *pStr)
{
char *pTmp = pStr;
int flag_0 = 0;
printf("*pTmp = %s\n",pTmp);
printf("*pTmp = %s\n",pTmp);
if(*pStr == '0')
{
pStr++;
flag_0 = 1;
}
while(*pStr != '\0')
{
if(*pStr != '.')
{
*pTmp++ = *pStr;
pStr++;
}
else
{
pStr++;
if(flag_0 ==1 && *pStr == '0')
{
pStr++;
}
}
}
*pTmp = '\0';
}
This code worked well and it print:
float str is 1.04
*pTmp = 1.04
*pTmp = 1.04
104
Code2:
int main()
{
char *tmp = "1.04";
printf("float str is %s\n",tmp);
delcharPoint(tmp);
printf("%s\n",tmp);
}
void delcharPoint(char *pStr)
{
char *pTmp = pStr;
int flag_0 = 0;
printf("*pTmp = %s\n",pTmp);
printf("*pTmp = %s\n",pTmp);
if(*pStr == '0')
{
pStr++;
flag_0 = 1;
}
while(*pStr != '\0')
{
if(*pStr != '.')
{
*pTmp++ = *pStr;
pStr++;
}
else
{
pStr++;
if(flag_0 ==1 && *pStr == '0')
{
pStr++;
}
}
}
*pTmp = '\0';
}
This code doesn't work,it print:
float str is 1.04
*pTmp = 1.04
*pTmp = 1.04
Segmentation fault
The difference between two codes only is I use char[] in code1 and char * in code2.I have searched the difference between char[] and char *.But I still don't what cause the difference between these code.
Here:
char *tmp = "1.04";
"1.04" is a string literal and string literals are immutable, meaning that it cannot be changed. Attempting to do so results in Undefined Behavior.
On the other hand,
char tmp[20] = "1.04";
creates a char array, copies "1.04" into it. Modifying the contents of an array is legal.
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 8 years ago.
The following code results in a seg fault, when running in GDB it appears when the memory is changed to decrease the character by 32.
#include <stdio.h>
char *upper(char *);
int main(void) {
char *my_word = "hello";
printf("Upper: %s\n", upper(my_word));
return 0;
}
char *upper(char *string) {
while(*string != '\0') {
*string -= 32;
string++;
}
return string;
}
When you use string++ at the end it will point to \0.
char *upper(char *string) {
while(*string != '\0') {
*string -= 32;
string++; // don't use this method here
}
return string; // it will return the address of \0
}
while returning it will return the address of \0. so it wont print anything.
Try the following changes-
#include <stdio.h>
#include<string.h>
char *upper(char *);
int main(void) {
char my_word[] = "hello";
printf("Upper: %s\n", upper(my_word));
return 0;
}
char *upper(char *string) {
int i;
for(i=0;string[i];i++)
string[i] -=32;
return string;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Getting Segmentation Fault
/* Reverse a string in place
*/
#import <stdio.h>
void reverse(char * str);
int main()
{
char * string = "This is a string.";
printf("%s\n", string);
reverse(string);
printf("%s\n", string);
}
void reverse(char * str)
{
char * start = str;
char * end = str;
if(0==*str)
return;
//Find the end
for(;0 != *(++end););
end--;
do
{
*end = *end ^ *start;
*start = *end ^ *start;
*end = *end ^ *start;
}while(++start < --end);
}
I'm not sure why this seg faults. Is is because I'm initializing my char * with a constant string?
You are trying to modify a string constant. Replace:
char * string = "This is a string.";
with
char string[] = "This is a string.";
to remedy that.