我正在读一本名为C Traps and Pitfalls的旧书,由A Koenig于1989年出版,就在第一个C标准之前.在其中,我找到下面的代码:
char *r, *malloc();
r = malloc(strlen(s) + strlen(t) + 1);
Run Code Online (Sandbox Code Playgroud)
第一行无法正确编译; 我使用CodeBlocks与MinGW编译它,这给我以下错误信息:
||=== Build: Debug in beta (compiler: GNU GCC Compiler) ===|
C:\Users\ADMIN\Desktop\beta\beta\main.c||In function 'main':|
C:\Users\ADMIN\Desktop\beta\beta\main.c|9|error: conflicting types for 'malloc'|
C:\Program Files (x86)\CodeBlocks\MinGW\include\stdlib.h|356|note: previous declaration of 'malloc' was here|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Run Code Online (Sandbox Code Playgroud)
我学到了一些C,但我不知道"char*malloc()"的含义以及编译器发出错误的原因.
这是使用while循环的程序,
#include <stdio.h>
int main()
{
int i;
i = 1;
while (i <= 32767)
{
printf("%d", i);
i = i + 1;
}
}
Run Code Online (Sandbox Code Playgroud)
你认为循环会无限期地执行吗?
我的问题是,C++中的"| ="是什么?我知道他们是按位运算符,但我不明白他们在这里做了什么:
gObj->Variable |= 0x1000000;
Run Code Online (Sandbox Code Playgroud)
另外,在这种情况下,"&"运算符是什么意思?
if ((gObj->Variable & 2) == 2)
{
do stuff
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,如何使指针p存储i的地址?printf()里面的"*(float*)p"是什么意思?
#include
void main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float *)p);
}
Run Code Online (Sandbox Code Playgroud) 让我们有一个名为Employee的结构:
struct Employee
{
int basicsalary;
int bonus;
int netsalary;
};
Run Code Online (Sandbox Code Playgroud)
让我们有一个函数原型如下:
int calc_NetSalary(struct Employee**,int);
Run Code Online (Sandbox Code Playgroud)
让main函数声明结构数组的变量,如下所示:
struct Employee emp[100];
Run Code Online (Sandbox Code Playgroud)
让函数在main中调用如下:
for (i = 0; i < n; i++)
{
emp[i].netsalary = calc_NetSalary(emp[i],n); // Warning is here
}
Run Code Online (Sandbox Code Playgroud)
让函数定义如下:
int calc_NetSalary(struct Employee** emp,int n)
{
int i;
for(i = 0; i < n; i++)
{
emp[i]->netsalary = emp[i]->basicsalary + emp[i]->bonus;
}
return emp[i]->netsalary;
}
Run Code Online (Sandbox Code Playgroud)
我不知道作为一个参数传递什么,该函数需要一个带有两个星号的参数(代替emp上面的).我正在收到警告passing argument 1 of ‘calc_NetSalary’ from incompatible pointer type.我知道这一定是一个愚蠢的错误,或者我不清楚指针的概念.请帮忙 !
我已经看到以这种方式声明的某些函数:
char* encipher(const char *src, char *key, int is_encode);
Run Code Online (Sandbox Code Playgroud)
我不明白这一部分:
char* encipher
Run Code Online (Sandbox Code Playgroud)
数据类型后的星号是什么意思?
我是编程新手,有些部分让我在学习指针时感到困惑.
代码1:
int main()
{
char string[]="hello";
char *my_pointer=string;
printf("first character is %c", *my_pointer);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:第一个字符是h
代码2:
int main()
{
char array_of_words[]="one\0two\0three";
char *my_pointer=array_of_words;
printf("%s \n",my_pointer);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:一
问题:
我在这里混淆,第一个代码中的printf函数部分使用星号符号表示指向内部指针(my_pointer),它是变量字符串的地址,引用指向内存地址的数组的指针字符串.数组中的第一个单词"hello".我的理解是真的吗?
当我在printf中更改%c时("第一个字符是%c",*my_pointer); 到%s,程序崩溃了.我想知道为什么我不能使用%s,如果我不使用printf中的my_pointer中的星号("第一个字符是%c",my_pointer),有什么不同?
在第二个代码中,my_pointer变量在{printf("%s \n",my_pointer);}中没有使用星号(*).在这段代码中my_pointer是指array_of_words变量的地址还是my_pointer里面的内容?以及为什么输出是'一'而不是'o'?
我刚刚开始用c编程,我知道python的一些基础知识,所以当我在python中为变量设置一个非常大的int值时说'58944651132156484651'并打印该变量,一切都会好起来,但是当我对c做相同的回答时,是247 ...将会出现很多问题,例如我无法将我的电话号码设置为变量,还有很多类似的事情,所以请帮助我解决这个问题。记住我是python用户,现在我正在学习c。
/*code in c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num = 456465465456456465465456;
printf("%d \n", num);
return 0;
}
#code in python
num = 54454564564848431284132116483211
print(num)
#that's better than the c's big print :(
Run Code Online (Sandbox Code Playgroud) 您必须创建包含DEBUG,TRUE,FALSE,NULL符号的包含文件.包含文件必须包含一个警卫.-->我不知道这是什么.当我读它时,我只看到中文.什么是包含文件,如何使其包含debug,true,false null,符号是什么?哈哈和你什么是"守卫".
提前致谢!