我想在分配动态内存后更改char指针的内容,是否可能?如果没有,为什么?我的程序抛出运行时错误.
#include <stdio.h>
int main()
{
char * str = (char *) malloc (10 * sizeof(char));
str = "Hello";
str[2] = 'L'; // here the program throws run time error
printf("%s", str);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在下面的情况下.更改字符串'out'是否分别更改字符串'str'?换句话说,他们有相同的指针吗?
先感谢您.
int main() {
char str[]={'g','o','o','d','/0'};
char special[]={'o','/0'};
char* out=str;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 c 学习递归,并且有一个关于数组的问题,例如:
void main(){
char str[100];
--snip--
if(*str == 'c')
count++;
--snip--
}
Run Code Online (Sandbox Code Playgroud)
这里的 *str 是从指向字符串str 的第一个字符的指针中检索到的值。
我的问题是什么是指针取消引用,我可以执行以下操作:
--snip--
str+=1;
--snip--
Run Code Online (Sandbox Code Playgroud)
获取指向该字符串中另一个字符的下一个位置的新指针?
为什么我在运行这个程序时遇到这个错误"Segmentation fault(core dumped)",它有什么问题?
#include <stdio.h>
int main() {
char *p = "Sanfoundry C-Test";
p[0] = 'a';
p[1] = 'b';
printf("%s", p);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我知道当我们宣布一种char *c ="Hello";手段,我们不能修改这个字符串.但是为什么我能够在C中修改这个字符串数组.
char *p [] = {"Hello","World"};
*p = "Mode";
Run Code Online (Sandbox Code Playgroud)
试图修改它不应该给我一个错误吗?
我是C的前任,所以请耐心等待我; 我知道我可以将数组语句作为*c或c[];
我的问题是关于memset:
char str[] = "hello!";
memset (str,'-',2);
puts (str);
Run Code Online (Sandbox Code Playgroud)
工作良好.但:
char *str = "hello!";
memset (str,'-',2);
puts (str);
Run Code Online (Sandbox Code Playgroud)
不工作,我知道char *str = ...是一个正常的数组语句.
如果有人可以帮我这个,我谢谢你!
我刚刚提出了一个问题,当我编译并在CodeBlockes中运行它时,我收到了一个错误,我无法运行它.
这是个问题.
该计划的输出是什么?
#include<stdio.h>
#include<string.h>
int main(){
int i, n;
char *x="Alice";
n = strlen(x);
*x = x[n];
for(i=0; i<=n; i++){
printf("%s ", x);
x++;
}
printf("\n", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
A.爱丽丝
B. ecilA
C.爱丽丝虱子冰
D. lice ice ce e