我写了一个算法来从字符串中提取单词并将它们存储在一个数组中,但是我得到了一个不需要的结果,我无法弄清楚为什么.输出是一个非常奇怪的字符.
#include <stdio.h>
#include <windows.h>
char *extract(char *String)
{
char str[10][20];
int x = -1,y = 0,z = 0;
for( size_t n = 0 ; n < strlen(String) ; n++ )
{
y++;
if( String[n] == ' ' ) y = 0, z = 0;
if( y == 1 ) x++;
if( y > 0 )
{
str[x][z] = String[n];
z++;
str[x][z] = '\0'; //Comment this*********
}
}
/*for( int n = 0; n < 10; n++ ) Uncomment this***** …Run Code Online (Sandbox Code Playgroud) 我在测试代码时遇到了一个问题.我定义了一个宏来获取数组元素的数量,如下所示:
#define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
此宏可用于计算初始化程序与存储容量匹配的数组的元素数(例如int buf[] = {1,2,3};),但对于声明为:的数组不是很有效:int buf[20] = {1,2,3};
现在我知道像这样计算数组元素非常简单,但是大量的元素呢?你怎么算他们?你知道,计数可能是一个杀手!
请考虑以下代码:
#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
void g_strcat(void *_Dst, size_t dstSize, size_t bytes, const void *_Src, size_t srcSize);
int main(void)
{
int dst[20] = { 1,2,3 };
int src[] = { 4,5,6 };
size_t dstSize = 3; // dstSize = ARRAY_SIZE(dst) doesn't work
size_t srcSize = ARRAY_SIZE(src);
g_strcat(dst, dstSize, sizeof(int), src, srcSize);
size_t n, newSize = dstSize + srcSize; …Run Code Online (Sandbox Code Playgroud) 我正在寻找这个问题的解释。我看不懂while部分,为什么会打印6。
#include <stdio.h>
#include<stdlib.h>
int main()
{
int array[] = {1, 2, 4, 0, 4, 0, 3};
int *p, sum = 0;
p = &array[0];
while (*p++)
sum += *p;
printf("%d\n", sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我的代码如下,问题也在下面解释为什么lvalue会发生;
typedef struct test_item
{
char id[MENU_NAME_LEN + NULL_SPACE];
MenuFunction function;
} test_item;
Run Code Online (Sandbox Code Playgroud)
和
typedef void (*MenuFunction)(VmSystem *);
Run Code Online (Sandbox Code Playgroud)
和
void display(VmSystem * system)
{
printf("test");
}
Run Code Online (Sandbox Code Playgroud)
我能够为我分配ID,但是当我尝试分配函数指针时,我得到以下错误lvalue作为一元&操作数
test_item test;
test.function = &display(system);
Run Code Online (Sandbox Code Playgroud) 为什么scanf需要额外的尺寸?喜欢有什么不对劲:
int main() {
printf("enter the first distance:\n");
printf("Feet: ");
scanf("%d\n", &d1.feet);
printf("Inch:");
scanf("%d\n", &d2.inch);
printf("enter the second distance:\n");
printf("Feet: ");
scanf("%d\n", &d2.feet);
printf("inch: ");
scanf("%d\n", &d2.inch);
sum.feet = d1.feet + d2.feet;
sum.inch = d1.inch + d2.inch;
printf("sum of distance: %d, %d", sum.feet, sum.inch);
return 0;
}
Run Code Online (Sandbox Code Playgroud)