输入第一个输入后,程序关闭
#include<stdio.h>
int main(void)
{
int biology,chemistry;
printf("\nEnter marks for maths");
scanf("%d",biology);
printf("\nEnter marks for tech1");
scanf("%d",chemistry);
return(0);
}
Run Code Online (Sandbox Code Playgroud) 当我明确说明字符串的值,然后将其与自身进行比较时,系统返回FALSE.这是否与系统添加的额外'\ 0'字符有关?我应该如何优化我的代码使其成为正确的?
char name[5] = "hello";
if(name == "hello")
{
...
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <string.h>
void print_reverse(char *s)
{
size_t len = strlen(s);
char *t = s + len - 1;
while (t >= s)
{
printf("%c", *t);
t = t - 1;
}
puts("");
}
Run Code Online (Sandbox Code Playgroud)
以上是一个在屏幕上向后显示字符串的功能.但我不明白第7行(char*t = s + len-1;).请问有人解释这是英语口语吗?
这可能是一个非常基本的问题,但在它进入我的脑海之后,我用谷歌搜索它,显然主要是搜索SO.不幸的是,他们都没有帮助我很多.实际上,在C中使用指针的想法似乎让我很困惑,或者可能是我不知道确切的目的.
不要与SO问题混淆: C++中指针变量和引用变量之间有什么区别?和我一起
我将提供一些示例来证明我的担忧:
第一点:
定义说的是这样的:
指针是包含另一个变量地址的变量.
所以,如果一个程序是这样的:
int i = 23;
printf("%d", i);
printf("%d", &i);
Run Code Online (Sandbox Code Playgroud)
而且,另一个程序是这样的:
int i = 23;
int *ptr;
ptr = &i;
printf("%d", *ptr);
printf("%d", ptr);
Run Code Online (Sandbox Code Playgroud)
上述两个程序都可以输出相同的内容.
如果指针也保留变量的地址,同时我们可以使用&符号获取变量的地址,我们不能通过导出任何变量的地址来执行相同的任务指针吗?我的意思是如果我没有将它声明为指针并将其用作int ptr = &i;第二代码片段并将其用作普通变量,那么会有什么区别?
第二点:
我在这里找到了:
C没有数组变量....但这实际上只是使用带有替代语法的指针.
这句话是否正确?由于我还是初学者,我无法验证任何有关此问题的陈述.但这对我来说有点混乱.如果该陈述也正确,那么这方面的实际解决方法是什么?它实际上是在后端工作的指针,只是编译器/ ide通过使用数组来愚弄我们(显然是为了保持简单性)?
所以,
最后,我可以看到,没有内存管理,没有其他特殊目的使用指针或有其他方法来做这些(可能我对这个问题有一些误解).另外,我还有另外一个问题,我们不能使用我们可以在&不使用指针的情况下使用符号获取的地址来执行内存管理任务吗?
我仍然是初学者,并且有一些关于C的基本知识(不完全),我缺乏关于指针的任何高级知识.所以,这些问题让我变得更糟.我几乎没有在SO中找到任何描述指针和正常变量的基本差异的帖子.因此,如果任何专家可以提供有关此的一些知识,将非常感谢.
此数组分配的 3D 版本似乎工作正常,但添加第四个维度显然会导致段错误。编辑:(事实证明指针的东西很好,只是内部循环中有一对拼写错误)
\n\n #include <stdio.h>\n #include <stdlib.h>\n\n #define M 2\n #define N 3\n #define O 4\n #define P 5\n\n int main()\n {\n int ****A = malloc(sizeof(int***)*M);\n int i, j, k, m;\n\n if(!A) \n {\n printf (" **** Out of Memory");\n }\n for(i = 0; i < M; i++)\n {\n A[i] = malloc(sizeof(int**)*N);\n if(!A[i]) \n {\n printf(" *** Out of Memory");\n }\n for(j = 0; j < N; j++)\n {\n A[i][j] = malloc(sizeof(int*)*O);\n if(!A[i][j]) \n {\n printf(" ** Out of …Run Code Online (Sandbox Code Playgroud) 这是Kernighan和Ritchie撰写的The C Programming Language的程序练习(第5章)。在此程序中,当我使用“ *”作为乘法运算符时,指针** argv指向argv [0],即1st(Zeroth)参数,并读取第一个字符“ P”而不是“ *”。
执行后,带有参数: ./ProgE5-10 +124 -3 * =
它返回错误的答案,而不是-372。
但是,如果将“ *”替换为“ x”,则程序可以正常运行。所有其他操作(即+,-,/,=)也可以正常工作。
请告诉我为什么*使argv指向程序名称。
//Exercise 5-10. Write the program expr, which evaluates a reverse Polish
//expression from the command line, where each operator or operand is a
//separate argument. For example, expr 2 3 4 + *
//evaluates 2 x C+4).
//For multiplication character '*' is not working
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define MAXLINE 1000
#define NUMBER 0
int sign = 1;
char s[MAXLINE]; …Run Code Online (Sandbox Code Playgroud) 可能重复:C中的
箭头运算符( - >)用法
据我所知,只有C++可以使用类(obj->某些东西)但是我在很多C应用程序中看到过这个运算符.
还有一个小问题.通常一个人使用C中的结构,如下所示:
structname.somevariable
Run Code Online (Sandbox Code Playgroud)
但是,我看到他们使用像:
structname.something1.something2
Run Code Online (Sandbox Code Playgroud)
它与关键字union有关吗?
那么,我从哪里开始呢.
我是编程世界的新手.我几乎不知道C的基础知识(而且我缺乏很多).我希望开始用C编写游戏,而不是用C++编写游戏,因为我发现C++比我想象的要困难得多.我查看了最近公开的Doom3的源代码,它是用C++编写的,我不理解代码,因为它非常OOP(ish).
我没有找到游戏的切入点,但是我在源代码中发现了多个int main并且感到困惑.
我读了游戏引擎,但是即使经过多次读取,我仍然无法理解它们.我其实不想做2D游戏,3D是我想做的,但我不知道怎么做.如何制作图形,当你走路时,一个物体是如何存在而不是消失再次重新绘制,如何模拟重力,以及更多.
如果我的问题令人困惑,我很抱歉,我总是在脑子里写下任何流行音乐.
这是使用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)
你认为循环会无限期地执行吗?
我试图使用for带有 ASCII 表的循环,通过用 32 减去字母数字来使字符串中的每个字符一一大写。但我不能i在 charstr和中使用 int str2。我怎样才能做到这一点?
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define STRLEN 200
void string_lower() {
}
void string_upper(char str) {
char str2;
int length = strlen(str);
for (int i = 0; i < length; i++) {
str2[i] = str[i - 32];
}
}
int main() {
char word[STRLEN] = { 0 };
char word1 = 97;
printf("Write a word");
fgets(word, STRLEN, stdin);
string_upper(word);
return 0;
}
Run Code Online (Sandbox Code Playgroud)