#include <stdio.h>
int main() {
int *p = 100;
int *q = 92;
printf("%d\n", p - q); //prints 2
}
Run Code Online (Sandbox Code Playgroud)
上述程序的输出不应该是8吗?
相反,我得到2.
我是初学者.我正在尝试编写一个程序来验证我将用一组给定的单词输入的一些单词然后进行比较.我在这段代码中使用的是迄今为止我学到的东西.请帮我理解这段代码有什么问题.
所以,当我输入一个单词,比如"flag"时,它会输出一个错误
"......第71行:1872年Sigmentation fault sh"$ {SHFILE} ...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
system("COLOR B0");
char *enter_flags[3];
char*available_flags[3]={"print","scan","flag"};
printf("\r\nEnther your flags here please: ");
for(int i=0;i<3;i++){
scanf("%s",&enter_flags[i]);
for(int j=0;j<3;j++){
if(strcmp(enter_flags[i],available_flags[j])==0)
{
printf("---%s---|---%s--- MATCH", enter_flags[i], available_flags[j]);
}
else printf("---%s---|---%s--- INCORRECT", enter_flags[i], available_flags[j]);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我理解了我的错误.谢谢你们!
在这部分代码中:
char* data;
char num[10];
sprintf(num, "%d", 260);
strcat(data, num);
strcat(data, "\0");
sprintf(num, "%d", 130);
strcat(data, num);
sprintf(num, "%d", 128);
strcat(data, num);
printf("Data: %s", data);
Run Code Online (Sandbox Code Playgroud)
它的印刷:
Data: 260130128
Run Code Online (Sandbox Code Playgroud)
为什么会这样?null终止符不应该终止打印?
谢谢
编辑:
数据已经初始化,我从一个函数中获取它.
为什么不打印只有260?我该怎么做?
初学者在这里试图了解bug的来源.
我已经写了这个递归函数来找到两个数字之间的二项式系数,这在概念上显然是正确的.然而,对于这两个数字,n = 4和k = 2,我应该得到6,而我实际得到16.任何想法为什么会发生这种情况?
#include<stdio.h>
int binomial(int n, int k)
{
if ((k = 0) || (k == n))
return 1;
if (k>n)
return 0;
return binomial(n - 1, k - 1) + binomial(n - 1, k);
}
int main()
{
int a, b, res;
a = 4;
b = 2;
res = binomial(a, b);
printf("The result is %d", res);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我不明白为什么这个简单的代码不会在 linux 上导致分段错误的情况下运行:
#include <stdlib.h>
struct entry
{
int value;
};
void initEntry(struct entry *entry)
{
entry = malloc(sizeof(struct entry));
entry->value = 0;
}
int main()
{
struct entry *list;
initEntry(list);
list->value = 5;
}
Run Code Online (Sandbox Code Playgroud)
删除最后一条指令后我可以运行程序 ( list->value = 5;)
我编译:
gcc main.c -o main
Run Code Online (Sandbox Code Playgroud) 我期待这个简单的代码编译:
void Foo(long a, long);
void Foo(long a, double b);
int main()
{
Foo(1, 2);
Foo(1, 2.0);
}
Run Code Online (Sandbox Code Playgroud)
编译器输出:
prog.cpp: In function ‘int main()’:
prog.cpp:8:11: error: call of overloaded ‘Foo(int, int)’ is ambiguous
Foo(1, 2);
^
prog.cpp:3:6: note: candidate: void Foo(long int, long int)
void Foo(long a, long);
^~~
prog.cpp:4:6: note: candidate: void Foo(long int, double)
void Foo(long a, double b);
^~~
Run Code Online (Sandbox Code Playgroud)
我不明白为什么电话是暧昧的.对我来说,Foo(unsigned long a, unsigned long)显然比更匹配void Foo(unsigned long a, double b)的Foo(1, 2).
但是,如果我取代 …
间隔设置为1秒的TTimer每1秒发送一条消息.此消息在应用程序的消息循环中处理,这会导致触发OnTimer事件.
如果应用程序繁忙且没有时间处理消息循环,则会跳过OnTimer事件.
我知道TTimer使用内部SetTimer.
我的问题是:
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Caption = i;
i++;
MessageDlg(stuff); <----- we "block" application here but form's caption is still updated.
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个定义结构的解决方案,用户可以在示例中启用/禁用struct成员(伪代码):
#define DEF_STRUCT_1(NAME,VAL1,VAL2) \
struct my_struct_t \
{ \
#if(NAME == TRUE) \
bool name; \
#endif \
#if(VAL1 == TRUE) \
bool val1; \
#endif \
#if(VAL2 == TRUE) \
bool val2; \
#endif \
} instance1
void main() {
DEF_STRUCT_1(TRUE,FALSE,TRUE);
instance1.name = true;
//instance1.val1 = false; // error, unavailable
instance1.val2 = false;
}
Run Code Online (Sandbox Code Playgroud) 当我这样给予时:
int main()
{
int a =123;
char *p =(char*)&a;
printf("%d\n",*p);
++p;
printf("%d\n",*p);
++p;
printf("%d\n",*p);
++p;
printf("%d\n",*p);
}
Run Code Online (Sandbox Code Playgroud)
我有输出像
123
0
0
0
Run Code Online (Sandbox Code Playgroud)
但我们这样给了:
int main()
{
int a =1234;
char *p =(char*)&a;
printf("%d\n",*p);
++p;
printf("%d\n",*p);
++p;
printf("%d\n",*p);
++p;
printf("%d\n",*p);
}
Run Code Online (Sandbox Code Playgroud)
我输出为
-46
4
0
0
Run Code Online (Sandbox Code Playgroud)
我对系统如何将值(123/1234)存储在4个字节的变量a中感到困惑.如果它小于127,则第一个字节占据我们作为输入的值(iw 123).
但是如果该值大于127,则变量a的第一个和第二个字节存储不同的值.请解释它是如何工作的?
作为我们在编程语言学院培训的一部分,我们也学习了C.在测试期间,我们遇到了程序输出的问题:
#include <stdio.h>
#include <string.h>
int main(){
char str[] = "hmmmm..";
const char * const ptr1[] = {"to be","or not to be","that is the question"};
char *ptr2 = "that is the qusetion";
(&ptr2)[3] = str;
strcpy(str,"(Hamlet)");
for (int i = 0; i < sizeof(ptr1)/sizeof(*ptr1); ++i){
printf("%s ", ptr1[i]);
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
后来,在检查了答案之后,很明显单元格(&ptr2)[3]与&ptr1 [2]中的存储单元相同,所以程序的输出是: to be or not to be (Hamlet)
我的问题是,是否有可能只通过笔记本中的书面代码,而不检查任何编译器,知道某个指针(或一般所有变量)跟随或先于内存中的其他变量?
注意,我不是指数组变量,因此数组中的所有元素必须按顺序排列.