所以目前我正在从 C 编程语言第二版书中学习 C,它说:
while (c = getchar() != EOF) {
}
Run Code Online (Sandbox Code Playgroud)
等同于:
while (c != EOF) {
c = getchar();
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行刚刚编写的代码时:
#include <stdio.h>
int main() {
char c;
int times;
while (c = getchar() != EOF) {
if (c == 'a') {
++times;
}
}
printf("%d\n", times);
}
Run Code Online (Sandbox Code Playgroud)
times它输出的值是 0 而不是我输入 'a' 字符的实际值。现在在这段代码中,它工作正常:
#include <stdio.h>
int main() {
char c;
int times;
while (c != EOF) {
c = getchar();
if (c == 'a') {
++times;
} …Run Code Online (Sandbox Code Playgroud) 我只是想问一下有什么区别
int *a = (int *)malloc(sizeof(int))
Run Code Online (Sandbox Code Playgroud)
和
int *a = malloc(sizeof(int))
Run Code Online (Sandbox Code Playgroud)
因为我在做项目时遇到了这种情况,我尝试创建一个数组作为指针,并动态为其分配内存。但是当我使用时,int * a = malloc(sizeof(int))我无法更改数组的任何值,从而导致分段错误,当我将其更改为int * a =(int *)malloc(sizeof(int))它时,一切都开始正常工作。更准确地说,这是有问题的代码:
int i = 0;
for (i = 0; i < valCounter; i++) {
if (strcmp(vars[i].name, name) == 0) {
return 2;
}
}
strcpy(vars[valCounter].name, name);
strcpy(vars[valCounter].type, type);
i--;
vars[i].intArrayVal = (int *)malloc((dimension + 5) * sizeof(int));
int j = 0;
char currentNumber[1000];
char elemente[1000];
int *pointer;
strcpy(elemente, elements);
strcpy(currentNumber, strtok(elemente, ","));
vars[i].intArrayVal[j++] = atoi(currentNumber);
while (currentNumber …Run Code Online (Sandbox Code Playgroud) 这是我用来学习指向结构体工作方式的代码。正如你在下面看到的,变量的值s不等于它指向的结构体的地址(&amity),奇怪的 *s是等于它。
我还有一些问题:
为什么所有结构实例(甚至&instance.name)的地址都相同?
虽然s和i和j相同,但为什么 的值*j与*s的*i?
最后,为什么结构体的值仅等于其第一个字段的值?
该代码是从《Head First C》一书中修改而来的。
#include <stdio.h>
typedef struct island {
char *name;
char *open;
char *close;
struct island *next;
} island;
void text(island *s);
int main(void) {
island amity = { "Amity", "09:00", "17:00", NULL };
island craggy = { "Craggy", "09:00", "17:00", NULL };
island isla_nublar = { "Isla Nublar", "09:00", "17:00", NULL }; …Run Code Online (Sandbox Code Playgroud) 下面是一些工作 - 16 位 x86 程序集
global _start
section .text
_start: mov ah, 0x0e
mov bp, 0x8000
mov sp, bp
push "A"
push "B"
push "C"
pop bx
mov al, bl
int 0x10
pop bx
mov al, bl
int 0x10
mov al, [0x7ffe]
int 0x10
jmp $
times 510 -( $ - $$ ) db 0
dw 0 xaa55
Run Code Online (Sandbox Code Playgroud)
我将 3 个值压入堆栈,然后打印前 2 个值。我试图通过间接寻址打印第三个值,因此 mov al, [0x7ffe] - 但问题是,因为堆栈的基数为 0x8000 并且只能弹出 16 位值,要打印的最后一个字符的地址不是,-“a”是 0x8000 - 0x1 = 0x7FFF 而不是 0x8000 …
我正在尝试打印一些包含 integer 的整数数据0,但我想忽略包含它的数据NULL而不打印它。但我的 C 代码无法区分0和NULL并将它们视为相同。
void ecrire(struct node *p) {
if (p->data == NULL) { 'if the first node contains NULL it means list is empty'
printf("no items!\n");
return;
}
struct node *temp = p;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
当我传递第一个节点中包含值 0 的列表时,它会将其视为空列表。请问有什么解决办法吗?
测试用例超时。可能使用无限循环或低效算法。 参数无法更改,某些测试用例失败。我哪里出错了?
int binarySearch(int p[], int n, int key) {
int l = 0, h = n - 1;
int mid = l + (h - l) / 2;
while (l <= h) {
if (p[mid] == key) {
return mid;
}
if (p[mid] < key) {
l = mid + 1;
}
if (p[mid] > key) {
h = mid - 1;
}
}
return -1;
}
Run Code Online (Sandbox Code Playgroud) = '-';或是什么= '/'意思strchr(我知道它确实定位了工作和strrchr最后一次出现的情况)?
这段代码确实创建了两个文件。
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fpA = fopen("output_A.txt", "w");
FILE *fpB = fopen("output_B.txt", "w");
char *strA = ",-/";
char temp[100];
char str[5][60] = { { "summer is coming!" },
{ "vacation will let you chill out" },
{ "and, have a nice time" },
{ "and, stay fit" },
{ "and, wish you the best" }, };
fprintf(fpA, "%s\n", str[0]);
fprintf(fpB, "%s\n", str[1]);
fclose(fpA);
fclose(fpB);
fpA …Run Code Online (Sandbox Code Playgroud) double max(int count, ...)我的程序中有一个函数。此函数应返回最大数字,但它报告-31 > 6. 我的错误在哪里?我正在尝试学习va_。我怎样才能解决这个问题?
double max(int count, ...)
{
double max = INT_MIN, test;
int i;
va_list values;
va_start(values, count);
for (i = 0; i < count; ++i)
{
test = va_arg(values, double);
if (test > max)
{
max = test;
}
}
va_end(values);
return max;
}
int main()
{
printf("%ld", max(5, 1, 6, -31, 23, 24));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我计算给定字符串的字符数的代码(即使用strlen()函数):
#include <stdio.h>
#include <string.h>
#define N 30
int main() {
char input[N];
gets(input);
int j = strlen(input);
printf("using library fnc strlen=%d", j);
int i = 0, sum = 0;
for (i = 0; input[i] != '\0'; i++) {
sum = sum + i;
}
printf("\rusing loop %d", sum);
}
Run Code Online (Sandbox Code Playgroud)
在Code::Blocks(gcc编译器)上,结果显示如下:
abcdef78
using loop 4y fnc strlen=8
Process returned 0 (0x0) execution time : 5.816 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)
但是Programiz的在线编译器显示是这样的:
/tmp/rzNX0Zm3SF.o
abcdef78
using library …Run Code Online (Sandbox Code Playgroud) 我编写了这段代码来实现合并排序,它从用户处获取输入数组并使用合并排序对该数组进行排序。但它会给出与输入相同的输出。
我还将临时数组 ( temp[]) 复制到array[].
请检查我的代码并给我解决方案,谢谢。
#include <stdio.h>
void merge(int array[], int mid, int low, int high) {
int i, j, k, temp[100];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high) {
if (array[i] < array[j]) {
temp[k] = array[i];
i++;
k++;
} else {
temp[k] = array[j];
j++;
k++;
}
}
while (i <= mid) {
temp[k] = array[i];
i++;
k++;
}
while (j <= …Run Code Online (Sandbox Code Playgroud)