小编use*_*000的帖子

理解printf中的符号

这对许多人来说可能是一个非常基本的问题,但我无法理解%.*s在做什么?

void substring(int i, int j, char *ch) 
{
      printf("The substring is: %.*s\n", j - i, &ch[i]); 
      //what is %.*s doing?
}
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
70
查看次数

为什么这个函数在无限循环中运行?

我想学习递归.我不明白为什么下面的代码在无限循环中运行?

void myFunc(int n)
{
     if(n==0)
         return;
     else
     {
         printf("%d\n",n);
         myFunc(n--); //if I put n=n-1 before this line then it is running fine and will exit from the function .
         printf("%d\n",n); 
     }

}

int main()
{

    myFunc(4);
}
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
121
查看次数

理解全局变量的概念

如果我们有一个全局定义的变量"x"和一个函数内具有相同名称"x"的另一个变量.当我们打印"x"的值为什么我们总是得到函数内部赋值的值?我们有什么方法可以打印全局变量值.

int x=8;
void testCode()
{
    int x=2;
    printf("%d",x); //prints 2
}
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
85
查看次数

memcpy的三维数组

我们可以使用memcpy以非常干净的方式将整个3d数组内容复制到另一个3d数组中.我想知道是否有更简洁的方法将3d数组的少量内容复制到另一个中.

void function()
{
    uint32_t array1[2][2][2];
    uint32_t array2[2][2][2];
    //memset both the arrays to 0 first;
    memset(&array1,0,sizeof(array1));
    memset(&array2,0,sizeof(array2));

    array1[0][1][1]= 4;
    array1[0][0][0]=6;
    //memcpy(&array2,&array1,sizeof(array1)); 

    memcpy(&array2[0][1][1],&array1[0][1][1],sizeof(array1[0][1][1])); //this works, but if there is a cleaner way?
    printf_all("value of array1 %d and array2 %d \n",array1[0][1][1],array2[0][1][1]);
}
Run Code Online (Sandbox Code Playgroud)

c arrays

1
推荐指数
1
解决办法
266
查看次数

变量可以在设置之前使用

创建指向结构的指针时,出现“在设置变量之前可以使用变量”的信息example *e。如果我改用变量example e,则不会收到错误消息。这是因为我没有为指针分配内存吗?

typedef struct example {
    int a;
}example;

void test (){

    example *e;
    e->a=1;
    printf_all("val %d",e->a);
}
Run Code Online (Sandbox Code Playgroud)

c malloc memory-management initialization

1
推荐指数
2
解决办法
3096
查看次数

存储大小未知:枚举

我在file1.h中定义了一个枚举.我想将此枚举作为参数在另一个文件file2.h中引用,而不包括file1.h.现在我必须从file3.h调用get_color()函数.我收到两种不同类型的错误:

  1. 从不兼容的指针类型[-Werror]传递'get_color'的参数1
  2. 错误1已解决,但我得到一个不同的错误:col的存储大小未知.

唯一的问题是我不能在file2.h中包含file1.h.请建议我如何解决这个问题.

file1.h

typedef enum {
    RED,
    BLUE,
    GREEN2,
} colors_t;
Run Code Online (Sandbox Code Playgroud)

file2.h

void get_color(enum colors_t *col);
Run Code Online (Sandbox Code Playgroud)

file3.h //选项1

#include "file1.h"
#include "file2.h" 
int main()
{
     colors_t col;
     get_color(&col); //error: passing argument 1 of 'get_color' from   incompatible pointer type [-Werror]

}
Run Code Online (Sandbox Code Playgroud)

file3.h //选项2

#include "file1.h"
#include "file2.h" 
int main()
{
     enum colors_t col;
     get_color(&col); //error: storage size of col isn't known.

}
Run Code Online (Sandbox Code Playgroud)

c enums

1
推荐指数
1
解决办法
322
查看次数

将宏转换为函数调用

我有一个创建的宏,它将打印所有打印的时间戳.

void timestamp()
{
    struct timeval tv;
    gettimeofday(&tv,NULL);
    printf("%d",tv.tv_sec );
}

#define printf_all(format, ...) {                           \
    static const char format_string[] = format; \
    printf(format_string, ##__VA_ARGS__);                  \
    timestamp(); \
}

int main()
{
    printf_all("%d\n",10);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想将此宏转换为函数.但是在传递论据时我遇到了问题.

void printf_timestamp(static const char format_string[]) {                           
    static const char format_string[] = format; 
    printf(format_string, ##__VA_ARGS__);                  
    timestamp(); 
}
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
219
查看次数

检查指针是否对齐64字节时出错

当我检查地址是否为64字节对齐时,出现编译错误。

错误:对二进制表达式无效的操作数(“ void *”和“ int”)

  #define BYTE_ALIGNMENT 64
  void *is_mem_aligned(void* ptr){

    if(ptr%BYTE_ALIGNMENT == 0){
          printf("already aligned %p\n",ptr);
          return ptr;

    }

}
Run Code Online (Sandbox Code Playgroud)

c pointers

0
推荐指数
1
解决办法
35
查看次数

设置一下后打印变量

我试图在8位变量中设置一点.每次在设置特定位后打印变量时,我总是将值设为1.

uint8 value;
value = (1<<1 || value)
printf(%x \n,value); //prints 1 instead of 2
value = (1<<2 || value)
printf(%x \n,value); //prints 1 instead of 4
Run Code Online (Sandbox Code Playgroud)

c bit-manipulation

-1
推荐指数
1
解决办法
64
查看次数

在反转字符串时交换指针值时出现分段错误

我试图扭转一个字符串.我拿了两个指针,一个指向字符串的开头,另一个指向字符串的末尾.然后我交换了指针的值.

int main()
{

    char *string = "stack overflow";
    int len = strlen(string);
    char *end;
    char tmp; //temporary variable to swap values

    //pointer pointing at the end of the string
    end = string+len-1;

    printf(" value of start and end %c %c", *string , *end); //correct values printed
    while(string<end)
        {

                tmp = *string;
                *string = *end; //segmentation fault
                *end = tmp;
                *string++;
                *end--;


        }


    return 0;

}
Run Code Online (Sandbox Code Playgroud)

c pointers

-2
推荐指数
1
解决办法
572
查看次数

如何找出启用的位数:位处理

在我给出的一次采访中提到了这一点.我无法正确回答这个问题.

我想知道根据数字启用了多少位.

假设,如果数字是2,我应该返回3.如果数字是3,我应该返回7

8 4 2 1 

    1 1 

8 4 2 1

  1 1 1
Run Code Online (Sandbox Code Playgroud)

这有什么简单的方法吗?

c

-3
推荐指数
1
解决办法
137
查看次数