以下代码在第2行接收seg错误:
char *str = "string";
str[0] = 'z'; // could be also written as *str = 'z'
printf("%s\n", str);
Run Code Online (Sandbox Code Playgroud)
虽然这非常有效:
char str[] = "string";
str[0] = 'z';
printf("%s\n", str);
Run Code Online (Sandbox Code Playgroud)
经过MSVC和GCC测试.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
//char s[6] = {'h','e','l','l','o','\0'};
char *s = "hello";
int i=0,m;
char temp;
int n = strlen(s);
//s[n] = '\0';
while (i<(n/2))
{
temp = *(s+i); //uses the null character as the temporary storage.
*(s+i) = *(s+n-i-1);
*(s+n-i-1) = temp;
i++;
}
printf("rev string = %s\n",s);
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译时,错误是分段错误(访问冲突).请告诉我们两个定义有什么区别:
char s[6] = {'h','e','l','l','o','\0'};
char *s = "hello";
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) 如果我定义如下的内容,
char *s1 = "Hello";
Run Code Online (Sandbox Code Playgroud)
为什么我不能做下面这样的事情,
*s1 = 'w'; // gives segmentation fault ...why???
Run Code Online (Sandbox Code Playgroud)
如果我做下面的事情会怎么样
string s1 = "hello";
Run Code Online (Sandbox Code Playgroud)
我可以做下面这样的事情,
*s1 = 'w';
Run Code Online (Sandbox Code Playgroud) 我的代码段错误,我不知道为什么.
1 #include <stdio.h>
2
3 void overwrite(char str[], char x) {
4 int i;
5 for (i = 0; str[i] != '\0'; i++)
6 str[i] = x;
7 }
8
9 int main(void) {
10 char *s = "abcde";
11 char x = 'X';
12 overwrite(s, x);
13 printf("%s\n", s);
14 return 0;
15 }
Run Code Online (Sandbox Code Playgroud)
gdb调试器告诉我,问题出在第6行,我想将一个char存储到c-string中(如果我使用左值指针解除引用,那就是同样的问题.)这就是他所说的:
(gdb) run
Starting program: /tmp/x/x
Breakpoint 1, overwrite (str=0x8048500 "abcde", x=88 'X') at x.c:5
5 for (i = 0; str[i] != '\0'; i++) …Run Code Online (Sandbox Code Playgroud) 我试图使用两个指向相同字符串的指针就地反转一个字符串.第一个(char *p)指向字符串的起始位置,第二个(char *q)指向字符串的结束位置.因此,当我尝试使用gdb进行调试时,我在第16行遇到了分段错误.
当我试图打印的价值*p和*q,它工作正常.为什么我仍然能够访问这些位置时遇到段错误?
Breakpoint 1, main () at reverse.c:16
16 *q = *p;
(gdb) print p
$1 = 0x5555555547e4 "hello"
(gdb) print q
$2 = 0x5555555547e8 "o"
(gdb) step
Program received signal SIGSEGV, Segmentation fault.
0x00005555555546db in main () at reverse.c:16
16 *q = *p;
Run Code Online (Sandbox Code Playgroud)
该程序的实际代码是
#include<stdio.h>
int main() {
char *array = "hello";
char *p=&array[0];// pointer to the first element
// Make q point to last value of the array …Run Code Online (Sandbox Code Playgroud) 尝试运行以下函数时出错:
char* reverseInPlace(char* src)
{
//no need to alloc or free memory
int i=0;
int size=mystrlen(src);
for(i=0;i<size;i++)
{
int j=size-i-1;
if(i<j)
{
char temp;
printf("Interchange start %d:%c with %d:%c",i,src[i],j,src[j]);
temp=src[i];
src[i]=src[j];//error occurs here
src[j]=temp;
printf("Interchange complete %d:%c and %d:%c",i,src[i],j,src[j]);
}
}
return src;
}
Run Code Online (Sandbox Code Playgroud)
我把这个代码称为:
char* rev2=reverseInPlace("BeforeSunrise");
printf("The reversed string is %s\n",rev2);
Run Code Online (Sandbox Code Playgroud)
错误如下所示:
Interchange start 0:B with 12:e
Process terminating with default action of signal 11 (SIGSEGV)
Bad permissions for mapped region at address 0x401165
Run Code Online (Sandbox Code Playgroud)
为什么会出现此错误?
这两个代码必须更改字符2中的char'4'
int main(int argc, char *argv[]){
char *s = "hello";
*(s+2)='4';
printf( "%s\n",s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我运行时会出现分段错误:
int main(int argc, char *argv[]){
char *s = argv[1];
*(s+2)='4';
printf( "%s\n",s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道还有其他方法可以做到这一点.两个程序有什么区别?
我在这里有一个程序,我正在尝试使用ceasar密码解码一串字母; 本质上我将字符串中的每个字符"向下"移动一个字母("a" - >"b","f" - >"g","z" - >"a").
我移动一封信的数量取决于我给它的关键.
在这个特定的程序中,我有一个秘密编码的消息硬编码到main()函数中,一个for循环遍历每个可能的密钥.
这个想法是,如果这个秘密信息只是向下移动了x个字母,吐出25个版本的密码将揭示一个可理解的答案.
不幸的是,我正在使用一些对我来说很新的概念 - argc,argv和一个程序中的多个函数.我对此很新.
有人可以帮我解释我得到的分段错误错误吗?我认为我没有引起任何参数溢出.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
string decode(int key, string message);
int main(void)
{
string secret_message = "ueuag rKJ AGIQ GIAR FEgN";
for (int i = 0; i < 25; i++)
{
decode(i, secret_message);
printf("%s", secret_message);
}
return 0;
}
string decode(int key, string message)
{
int i;
for (i = 0; i < strlen(message); i++)
{
if (message[i] >= 'a' && message[i] <= 'z') …Run Code Online (Sandbox Code Playgroud)