可能重复:
为什么在写入字符串时会出现分段错误?
我想编写一个函数来反转传递给它的给定字符串.但是我不能.如果我doReverse使用字符数组提供函数(请参阅下面的代码),我的代码运行良好.
我无法弄清楚为什么这不起作用.我能够访问str[0]的doReverse,但我不能用一个字符指针改变数组中的任意值.有任何想法吗?
void doReverse(char *str) {
str[0] = 'b';
}
void main(void) {
char *str = "abc";
doReverse(str);
puts(str);
}
Run Code Online (Sandbox Code Playgroud)
我知道如何通过将字符数组传递给它来编写反向函数:
void reverse1(char p[]) {
int i, temp, y;
for (i = 0, y = strlen(p); i < y; ++i, --y) {
temp = p[y-1];
p[y-1] = p[i];
p[i] = temp;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想编写另一个获取char指针作为参数的版本.
这个问题只是为了让我更好地理解C++中的静态变量.
我认为如果它被声明为静态,我可以在C++中返回对局部变量的引用,因为变量应该在函数返回后生效.为什么这不起作用?
#include <stdio.h>
char* illegal()
{
char * word = "hello" ;
return word ;
}
char* alsoNotLegal()
{
static char * word = "why am I not legal?" ;
return word ;
}
int main()
{
// I know this is illegal
//char * ill = illegal();
//ill[ 0 ] = '5' ;
//puts( ill ) ;
// but why is this? I thought the static variable should "live on" forever -
char * leg = alsoNotLegal() ; …Run Code Online (Sandbox Code Playgroud) 我正在学习C,我正在尝试使用指针来反转字符串.(我知道你可以使用一个数组;这更多的是关于学习指针.)
尝试运行下面的代码时,我不断遇到分段错误.海湾合作委员会似乎不喜欢这*end = *begin;条线.这是为什么?
特别是因为我的代码几乎与另一个问题中讨论的非邪恶C函数相同
#include <stdio.h>
#include <string.h>
void my_strrev(char* begin){
char temp;
char* end;
end = begin + strlen(begin) - 1;
while(end>begin){
temp = *end;
*end = *begin;
*begin = temp;
end--;
begin++;
}
}
main(){
char *string = "foobar";
my_strrev(string);
printf("%s", string);
}
Run Code Online (Sandbox Code Playgroud) C99标准是否允许写复合文字(结构)?它似乎不提供写文字字符串.我问这个是因为它在C编程:现代方法,第 406页第2版中说.
问:允许指向复合文字的指针似乎可以修改文字.是这样的吗?
答:是的.复合文字是可以修改的左值.
但是,我不知道它是如何工作的,以及如何使用你当然无法修改的字符串文字.
char *foo = "foo bar";
struct bar { char *a; int g; };
struct bar *baz = &(struct bar){.a = "foo bar", .g = 5};
int main () {
// Segfaults
// (baz->a)[0] = 'X';
// printf( "%s", baz->a );
// Segfaults
// foo[0] = 'a';
// printf("%s", foo);
baz->g = 9;
printf("%d", baz->g);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您可以在我的列表中看到段错误,写入会baz->a导致段错误.但是,写作baz->g没有.为什么其中一个会导致段错而不是另一个?struct-literals与字符串文字有何不同?为什么struct-literals也不会被放入内存的只读部分,并且这两个行为是否定义或未定义(标准问题)?
为什么这段代码会崩溃?strcat在字符指针上使用非法?
#include <stdio.h>
#include <string.h>
int main()
{
char *s1 = "Hello, ";
char *s2 = "world!";
char *s3 = strcat(s1, s2);
printf("%s",s3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请在引用数组和指针时给出正确的方法.
只是为了测试我创建了以下代码:
#include<stdio.h>
int main(){
char *p = "Hello world";
*(p+1) = 'l';
printf("%s", p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但当我在ubuntu 10.04下运行我的"gcc"编译器时,我得到了:
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
所以任何人都可以解释为什么会这样.
#include<stdio.h>
#include<stdlib.h>
int main(){
char *p = malloc(sizeof(char)*100);
p = "Hello world";
*(p+1) = 'l';
printf("%s", p);
free(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这也导致分段错误在此先感谢
可能重复:
为什么在写入字符串时会出现分段错误?
我想编写一个简单的C++函数是一个反转
string/ char[]只指针运算.我理解这个概念并且已经输入了代码.
我有以下.cpp文件:
#include <iostream>
using std::cout;
using std::endl;
void reverse(char* target) //Requirements specify to have this argument
{
cout << "Before :" << target << endl; // Print out the word to be reversed
if(strlen(target) > 1) // Check incase no word or 1 letter word is placed
{
char* firstChar = &target[0]; // First Char of char array
char* lastChar = &target[strlen(target) - 1]; //Last Char of char array
char temp; // Temp …Run Code Online (Sandbox Code Playgroud) 对于c样式的字符串,如何将字符指针指向的内存地址赋予char?例如,在下面的示例中,我想将num更改为"123456",因此我尝试将p设置为'0'所在的数字,并尝试用'4'覆盖它.谢谢.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* num = (char*)malloc(100);
char* p = num;
num = "123056";
p = p+3; //set pointer to where '4' should be
p = '4';
printf("%s\n", num );
return 0;
}
Run Code Online (Sandbox Code Playgroud) 是否可以在C++中创建可修改的字符串文字?例如:
char* foo[] = {
"foo",
"foo"
};
char* afoo = foo[0];
afoo[2] = 'g'; // access violation
Run Code Online (Sandbox Code Playgroud)
这会产生访问冲突,因为"foo"被分配在只读存储器中(我相信.rdata部分).有没有办法强迫"foo"进入可写内存(.data部分)?即使通过一个pragma也是可以接受的!(Visual Studio编译器)
我知道我可以做strdup和其他一些事情来解决这个问题,但是我想特别知道我能不能按照我的要求去做.:)
我正在做一个练习,其中字符指针数组用作存储单词的一种方式。我不明白为什么我不能使用 'strcpy' 将单词 'hoi' 复制到主函数中数组的第二个元素。当我编译代码时,我在 CodeBlocks 中收到消息“程序已停止工作”。
函数“numberOfWordsInDict”和“printDict”工作正常。
提前致谢。
int numberOfWordsInDict(char **dict)
{
int i, cnt = 0;
for(i = 0; i < 10; i++)
{
if(dict[i] != NULL)
{
cnt++;
}
}
return cnt;
}
void printDict(char **dict)
{
int i = 0;
printf("Dictionary:\n");
if(numberOfWordsInDict(dict) == 0)
{
printf("The dictionary is empty.\n");
} else
{
for(i = 0; i < 10; i++)
{
printf("- %s\n", dict[i]);
}
}
}
int main()
{
char *dict[10] = {
"aap", "bro ", …Run Code Online (Sandbox Code Playgroud)