我试图用一些整数初始化一个2d数组.如果我将数组初始化为0我得到正确的结果但是如果我使用其他整数我得到一些随机值.
int main()
{
int array[4][4];
memset(array,1,sizeof(int)*16);
printf("%d",array[1][2]); <---- Not set to 1
}
Run Code Online (Sandbox Code Playgroud) 我怎样才能让它工作?
color_table = {"Red":[1,2,3], "Blue":[4,5,6]}
Run Code Online (Sandbox Code Playgroud)
如何单独访问值?
color_table[Red].0 = 1
color_table[Red].1 = 2
color_table[Red].2 = 3
color_table[Blue].0 = 4
color_table[Blue].1 = 5
Run Code Online (Sandbox Code Playgroud)
我想将这些值分配给一个变量。例如:
x = color_table[Red].0
Run Code Online (Sandbox Code Playgroud) static是全局变量的默认存储类.下面的两个变量(Count和Road)都有static存储类.
static int Count;
int Road;
int main()
{
printf("%d\n", Road);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如果默认情况下全局变量static(这意味着我们将该全局变量的范围限制为该特定.c文件)那么我们如何extern在另一个文件中使用这些变量?
这个问题对你们很多人来说可能是非常基本的,但我真的很困惑,想要了解正确的细节.
我必须为静态变量分配一个我从函数中获取的值.我试着做以下但我得到的初始化元素不是常数.
int countValue()
{
return 5;
}
void MatrixZero()
{
static int count=countValue();
count++;
printf("count value %d \n",count);
}
int main()
{
MatrixZero();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在网上找到了这个示例代码,它解释了该qsort函数的工作原理.我无法理解比较函数返回的内容.
#include "stdlib.h"
int values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void * a, const void * b) //what is it returning?
{
return ( *(int*)a - *(int*)b ); //What is a and b?
}
int main(int argc, _TCHAR* argv[])
{
int n;
printf("Before sorting the list is: \n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
qsort(values, 5, sizeof(int), cmpfunc);
printf("\nAfter sorting the list is: …Run Code Online (Sandbox Code Playgroud) 如何使用一行代码设置结构成员?
typedef struct {
int x;
int y;
}test;
test obj[2];
int main()
{
obj[0].x=0; //this works
obj[0].y=0; //this works
obj[0]={0,0}; //compilation error
}
Run Code Online (Sandbox Code Playgroud) 为什么~10的结果是-11?不应该是5,因为〜操作翻转每一点.
10 = 1010
~10 = 0101 // 5十进制
#include<stdio.h>
int main()
{
unsigned int b =10;
b= ~b;
printf("bitwise %d\n ",b);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我见过代码,人们将使用const作为函数的参数.使用const*vs const*const有什么好处?这可能是一个非常基本的问题,但如果有人能够解释,我将不胜感激.
Bool IsThisNumberEqualToFive(int const * num)
{
return(Bool)(5 != num );
}
Bool IsThisNumberEqualToFive(int const * const num)
{
return(Bool)(5 != num );
}
Run Code Online (Sandbox Code Playgroud) 我想了解arm链接寄存器是如何工作的以及它对调试有何帮助。我首先编写了一个简单的函数。
#define MACRO_TEST() (event_log__add_args(MACRO_TEST,__return_address()))
static void do_print_r14(void) {
printf_all("return address 0x%08X \n",__return_address()); //prints 0x823194BB
MACRO_TEST();
printf_all("return address 0x%08X \n",__return_address()); //prints 0x823194BB
}
Run Code Online (Sandbox Code Playgroud)
事件日志打印以下内容: 返回地址:0x0000ABAB
我的问题是为什么 do_print_r14 函数中的打印输出相同的值。如果我只登录行号和函数名称,这将指向代码的确切位置,这不是更有帮助吗?为什么开发者在调试时使用r14?
这个问题对你们来说可能听起来非常基本,但我完全不确定为什么我们需要 r14 寄存器。
To understand more about padding I created this structure. Why is the output of state 0x1413 and not 0x1615. I am expecting compiler will pad 3 bytes after zip and should give output as 0x1615.
typedef struct
{
uint8_t zip; // I am assuming 3 bytes will be padded after this.
uint16_t state[2]; // 4 bytes
uint32_t country; //4 bytes
} address;
int main()
{
uint8_t buf[] = {0x11, 0x12, 0x13, 0X14, 0x15, 0x16,0x17, 0x18, 0x09, 0x0A, 0x0B, 0x0C, 0X0D, …Run Code Online (Sandbox Code Playgroud)