小编chq*_*lie的帖子

将volatile变量作为函数中的常量参数传递

我正在为Cortex-M0 MC开发和嵌入代码,其中我声明了一个变量,volatile char TOS_Mins_Char[3]; 以便在ISR期间存储一些值,这些值会定期更改.我想使用atoi()函数将这些字符转换为整数,但atoi()将其参数类型作为指向常量字符串的指针:int atoi(const char *);除非我volatile在变量声明中避免使用关键字,否则这会给出错误.(在其他std函数中也面临类似的情况)

  1. 除了编写用户定义的函数之外,还有其他解决方案吗?
  2. 如果我使用const char TOS_Mins_Char[3];,它会有问题吗?
  3. 是否必须使用volatile关键字,与ARM MC相比,它的用途是什么?

c embedded arm

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

3路快速排序(C实现)

我尝试使用C 实现一些纯通用的算法.我坚持使用3向快速排序但不知何故实现不能提供正确的输出.输出几乎排序,但有些键不在应有的位置.代码如下.提前致谢.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

static void swap(void *x, void *y, size_t size) {
    void *tmp = malloc(size);

    memcpy(tmp, x, size);
    memcpy(x, y, size);
    memcpy(y, tmp, size);

    free(tmp);
}

static int cmpDouble(const void *i, const void *j) {
    if (*(double *)i < *(double *)j)
        return 1;
    else if (*(double *)i == *(double *)j)
        return 0;
    else 
        return -1;
}

void qsort3way(void *base, int lo, int hi, size_t size,
               int (*cmp)(const void *, const void …
Run Code Online (Sandbox Code Playgroud)

c sorting partitioning quicksort

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

按C中的流行度排序字符

我写了一个代码来列出字符串中字符的出现.它有效,但我想知道,是否有可能把它整理好?例如下降.听起来很简单,但我在这里使用了两个数组,我不知道怎么可能将它们链接起来,这样它们就不会在排序后搞砸了.我读到在C++中我可以使用std :: pair但是从我发现的内容来看,在C++中没有替代品.

是不是只有一个简单的方法来排序它,以便我可以有一个大多数/最不受欢迎的字符列表?我开始进入冒泡排序,但无论我选择什么,两个阵列之间仍然没有链接.

这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

main(int argc, char **argv) {
    int hits[26] = { 0 };
    char letters[26] = {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
        'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    char *line;
    int i;

    printf("Write the line:\n");
    scanf("%25[^\n]", line);
    for (i = 0; i < strlen(line); i++) {
        if (!isalpha(line[i]))
            continue;
        hits[(int)(tolower(line[i]) - 'a')]++;
    }

    for (i …
Run Code Online (Sandbox Code Playgroud)

c sorting

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

在C中两个连续的printf()调用的奇怪行为

我在玩C; 看一下这个:

#include <stdio.h>
#include <stdlib.h>

void main() {
    printf("%d\n", 1.5);
    printf("%f", 0);
}
Run Code Online (Sandbox Code Playgroud)

我期待输出:

0
0.000000
Run Code Online (Sandbox Code Playgroud)

但它打印:

0
1.500000
Run Code Online (Sandbox Code Playgroud)

第一次printf()通过1.5第二次printf()吗?

PS:我知道(%d对于整数,%f浮标).正如我所提到的,我只是在搞乱代码.

PS2:我正在使用DevC++和Code :: Blocks.

c printf

5
推荐指数
2
解决办法
236
查看次数

c中数组初始化的背景会发生什么?

鉴于这是合法的

uint8_t bytes[4] = { 1, 2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)

这不是:

uint8_t bytes2[4];
bytes2 = { 1, 2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)

什么{ 1, 2, 3, 4 }代表?

假设它既不是右值也不是左值.一个预处理器代码糖果扩展到什么?

c arrays initialization variable-assignment

5
推荐指数
3
解决办法
165
查看次数

计算交替的上/下序列

问题描述:计算从某个输入n向上的所有序列的数量.所以用户输入n; 然后我创建一个数字1..n数组,然后用该属性编号序列

例: n = 4

1 3 2 4
1 4 2 3
2 3 1 4
2 4 1 3
3 4 1 2
Run Code Online (Sandbox Code Playgroud)

回答: 5

我的程序有效,但出于某种原因,我有时会得到0而不是答案.

#include <stdio.h>
#include <stdlib.h>

void *safeMalloc(int n) {
    void *p = malloc(n);
    if (p == NULL) {
        printf("Error: malloc(%d) failed. Out of memory?\n", n);
        exit(EXIT_FAILURE);
    }
    return p;
}

void swap(int *fir, int *sec) {
    int temp = *fir;
    *fir = *sec;
    *sec = temp;
}

void permute(int *array, int i, …
Run Code Online (Sandbox Code Playgroud)

c algorithm recursion

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

从字符串中打印偶数和奇数字符的程序

这是为家庭作业

我必须编写一个程序,要求用户输入一个字符串,然后我的程序将输入的字符串中的偶数和奇数值分开.这是我的计划.

#include <stdio.h>
#include <string.h>

int main(void) {
    char *str[41];
    char odd[21];
    char even[21];
    int i = 0; 
    int j = 0;
    int k = 0;

    printf("Enter a string (40 characters maximum): ");
    scanf("%s", &str);

    while (&str[i] < 41) {
        if (i % 2 == 0) {
            odd[j++] = *str[i];
        } else {
            even[k++] = *str[i];
        }
        i++;
    }

    printf("The even string is:%s\n ", even);
    printf("The odd string is:%s\n ", odd);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译我的程序时,我得到两个警告:

  1. 因为我scanf得到了"format …

c arrays string

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

是否有属性告诉编译器忽略丢失的return语句?

我不认为这是重复的,因为该函数的确返回了快乐路径。使用该属性no-return可使编译器在该函数永不返回的假设下进行优化,此处并非如此。

我有C代码,它要么返回指针,要么调用另一个函数退出程序。这是在if语句中,因此它要么返回结果,要么退出。当函数返回a时void *,编译器警告该函数可能不会返回值(这当然是对的):

error: control reaches end of non-void function [-Werror=return-type]

我可以通过仅添加return *temp;到函数的末尾来解决此问题,但是我想通过使用未使用的变量属性之类的东西来弄清楚我的意图:

__attribute__((__unused__))

这样,我可以-Wall继续使用,而不必添加不必要的或可能引起混淆的代码。

如果有更好的方式表达这种意图,我也愿意重写代码。

该代码如下所示:

void *get_memory() {
    void *temp = malloc(100);
    if (temp) {
        // do some setup work
        return temp;
    } else {
        exit_program_with_epic_fail();
    }
    // Compiler warns if the following line isn't present
    return temp;
}
Run Code Online (Sandbox Code Playgroud)

c

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

我们可以使用 C 预处理器 ## 运算符粘贴 3 个标记吗?

似乎可以定义一个宏来连接 3 个标记为:

#define concat3(a,b,c) a##b##c
Run Code Online (Sandbox Code Playgroud)

中间令牌(例如由生成的令牌a##bb##c必须是有效的预处理器令牌)是否可以concat3(.,.,.)成功粘贴到所有符合要求的实现上?(1)


(1) 许多编译器支持..作为 case 范围的标记,这使其成为有效标记,但是 C 标准没有定义这个标记,那么concat3()宏会在不支持它的架构上失败吗?

c concatenation language-lawyer c-preprocessor

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

Malloc,strlen,strcat

以下是我正在编写的程序开头的一段代码(包含错误).

    char *name;
    char *name2;

    if (argn != 2) {
        printf("You have to enter the name of the input file");
        return 1;
    }

    name = malloc(strlen(arg[1]) + 1);      
    name2 = malloc(strlen(arg[1]) + 1);

    strcpy(name, arg[1]); 
    strcpy(name2, arg[1]);

    strcat(name2, "-results.pdb");  
Run Code Online (Sandbox Code Playgroud)

这里有一个错误strcat,实际上name2没有足够的大小来执行上面的操作.然而strcat执行没有问题.但是后来在程序的一个完全不相关的部分中,在此之后初始化的另一个数组的操作strcat给出了错误.它是一个整数数组,我为其分配值,并在分配所有值之前给出错误.我假设因为上面的操作在name2中没有足够的内存,所以"某种程度上"会影响下一个初始化的数组.我想了解:

1-这里可能发生什么,以便无法写入name2的额外信息会影响稍后声明的其他数组?

2-我可能无法在更复杂的程序中轻易地回溯这个问题,因为错误发生在其他地方而不是strcat中.我怎样才能防止这种偷偷摸摸的错误,比如memory problematic影响其他地方完全不相关的数组?

c arrays memory-management

4
推荐指数
2
解决办法
682
查看次数