我正在阅读Kernighan Ritchie并且有这个角色计数程序,所以我尝试了实施
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int i;
c = getchar();
while (c != EOF)
i= i + 1;
printf("%d",i);
}`
Run Code Online (Sandbox Code Playgroud)
当我编译并运行此代码时,在输入一些字符后,之后没有输出.没有打印数字,我无法弄清楚原因.代码看起来很好.我也试过使用scanf(),但同样的事情发生了.
下一个例子是计算输入中的行数,同样的问题也存在.
我有一个2维数组,当我第一次打印数组的数据时,日期打印正确,但另一次数组[last] [i]的数据从i = 0到last - 1.
显然是一个逻辑错误,但我不明白原因,因为我复制并粘贴了for语句.那么......¿C改变数据?
我使用gcc -std=c99但在此之前我尝试使用C++和cout语句.
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned int numero_jugaderes = 11;
unsigned int numero = numero_jugaderes - 1;
unsigned int p_a[numero];
float p_aya[numero][numero];
for (unsigned int i = 0; i <= numero; i++) {
p_a[i] = i;
}
for (unsigned int i = 0; i <= numero; i++) {
for (unsigned int j = 0; j <= numero; j++) {
p_aya[i][j] = (float) (p_a[i] …Run Code Online (Sandbox Code Playgroud) 我希望能够在c中打印出charcter的值,如下所示:
fprintf("%c", alphabet[val]);
Run Code Online (Sandbox Code Playgroud)
,其中字母表被初始化为
for(i = 0; i < 26; i++){
alphabet[i] = i + 65;
}
Run Code Online (Sandbox Code Playgroud)
但是,此行会出现以下错误:
encode.c: In function ‘main’:
encode.c:76:13: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
fprintf("%c", alphabet[val]);
^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’
extern int fprintf (FILE *__restrict __stream,
^
encode.c:76:19: warning: passing argument 2 of ‘fprintf’ makes pointer from integer without a …Run Code Online (Sandbox Code Playgroud) 据我所知,以下代码可用于获取数组的长度(表示它包含多少项)(在本例中为整数数组):
int arrayOfInt[] = { 10, 20, 30, 40, 50 };
int lenght = sizeof(arrayOfInt) / sizeof(int);
Run Code Online (Sandbox Code Playgroud)
这给出"5"作为输出,这是正确的.但是,如果我在函数中使用相同的代码,则它不起作用.
int getLenght(int intArray[])
{
return sizeof(intArray) / sizeof(int);
}
lenght = getLenght(arrayOfInt);
Run Code Online (Sandbox Code Playgroud)
使用上面的函数,我得到"1"作为输出.哪个不对.为什么会这样?我做错了什么或不可能?
switch(at){
case (at>0 && at<5) :
printf("Average Time Taken (Hrs)\n%d.0",at);
printf("Your Salary is Rs.%d",pj*1500 + 5000);
break;
Run Code Online (Sandbox Code Playgroud)
其余的代码是相似的.我收到了这个错误case (at>0 && at<5) :
在我不小心使用的代码中
list* Head = malloc(sizeof(list*));
Run Code Online (Sandbox Code Playgroud)
而不是正确的
list* Head = malloc(sizeof(list));
Run Code Online (Sandbox Code Playgroud)
创建一个新的list类型节点,但以后工作得很好.
所以我的问题是为什么它能正常工作?
如何释放动态分配的内存?
假设输入(假设它是由用户给出)是1000,现在如果我分配1000的内存,在此之后(第二次)如果用户输入为500,我可以重用已经分配的内存吗?
如果用户现在输入值为3000,我该如何使用它?我可以重用已分配的1000块内存,然后创建另外2000块内存吗?或者我应该创建所有3000块内存?
哪一个是可取的?
#include <stdio.h>
#include <stdlib.h>
typedef struct a
{
int a;
int b;
}aa;
aa* ptr=NULL;
int main() {
//code
int input=2;
ptr=malloc(sizeof(aa)*input);
for(int i=0;i<input;i++)
{
ptr[i].a=10;
ptr[i].b=20;
}
for(int i=0;i<input;i++)
{
printf("%d %d\n",ptr[i].a,ptr[i].b);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我对这种行为感到很困惑:
int main(void) {
char *test_string = "test_string";
*test_string = 'a'; //segfaults
return 0;
}
Run Code Online (Sandbox Code Playgroud)
int main(void) {
char test_string[] = "test_string";
*test_string = 'a'; //OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我预计它应该有相同的工作,因为N1570, Section 6.3.2.1(p3)声明如下:
除非它是运算
sizeof符,_Alignof运算符或unary &运算符的操作数,或者是用于初始化数组的字符串文字,否则具有类型''数组'类型'的表达式将转换为类型为''指针的表达式键入''指向数组对象的初始元素,而不是左值.
为什么第一个段错?
如果我运行此代码:
float a=1.123456789;
printf("The float value is %f\n",a);
double b=1.123456789876543
printf("The double value is %lf",b);
Run Code Online (Sandbox Code Playgroud)
它打印:
浮点值为1.123457
双精度值为1.123457
第一行是可以理解的,因为float的精度是~6位十进制数.但是第二行不应该显示更多数字吗?我正在使用Turbo C++ 4.0 for Windows,如果这有帮助的话.
#include <stdio.h>
#define CHAR_ROW_SIZE 4
int charTable[CHAR_ROW_SIZE ][2] = {
{'X', 'Z'},
{'J', 'L'},
{'F', 'C'},
{'A', 'B'}
};
int main()
{
printf("char element %c\n", charTable[3][1]); //fine
printf("char element %c\n", charTable[3][8]); // accessing 4th row's 9th element which is not valid
printf("char element %c\n", charTable[85][0]);// accessing 86th row's first element which is not valid
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
char element B
char element
char element
Run Code Online (Sandbox Code Playgroud)
根据我的理解,C\C++ 实际上并没有对数组进行任何边界检查。这取决于操作系统以确保您正在访问有效的内存。所以这是未定义的行为。
但是在这里我可以在不同的机器上看到相同的行为。即程序不会随时崩溃。