小编chq*_*lie的帖子

在 C 中创建多维 char 数组

我正在处理一个关于charC 中多维数组的问题。休息一段时间后,我很难记住所有内容。试着找出我做错了什么。平台是Ubuntu 21.10,编译器是gcc,代码如下。任何帮助我将不胜感激。

#include <stdio.h>

int main() {
    char *a1 = "hello1";
    char *a2 = "hello2";
    char b[][2] = { a1, a2 };
    printf("%s\n", b[1]);
    getchar();
    return (0);
}
Run Code Online (Sandbox Code Playgroud)

c

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

C++ MergeSort 如何返回任何内容但工作正常?

这就是下面合并排序的实现。但是,我不明白这段代码是如何工作的。我们不使用指针,并且 中不会返回任何内容main()。那么,它是如何操纵的呢myarray?谁能解释一下吗?

这是代码:

#include <iostream>
using namespace std;

void merge(int arr[], int l, int m, int r) {
  int i = l;
  int j = m + 1;
  int k = l;

  /* create temp array */
  int temp[5];

  while (i <= m && j <= r) {
    if (arr[i] <= arr[j]) {
      temp[k] = arr[i];
      i++;
      k++;
    } else {
      temp[k] = arr[j];
      j++;
      k++;
    }
  }

  /* Copy the remaining elements of first half, …
Run Code Online (Sandbox Code Playgroud)

c++ arrays mergesort pointers

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

为什么这段 C 代码没有为我计算数组中的元素数量?

无法计算数组中元素的数量

#include <stdio.h>

int counter(int arr[])
{
    int c =0;
    for (int i = 0; arr[i] != '\0'; i++)
    {
        c = c + 1;
    }
    return c;
}

int main()
{
    int a[] = { 1, 5, 3 };
    int d = counter(a);
    printf("%d ", d);

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

任何人都可以帮助我如何获得 3 作为其输出吗?

c arrays for-loop sizeof function-definition

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

如何将两个 25 位数字相加?

我正在尝试在 C 中添加大约 25 位数字。我得到的结果与预期的、可能的原因数据类型有点不同。

/* Online C Compiler and Editor */
#include <stdio.h>

int main()
{
    long double result;
    long double a;
    long double b;
    a = 51680708854858333333;
    b = 83621143489848333333,
    result = a + b;
    printf("Hello, World!\n");
    printf("can be written %.0Lf\n", result);

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

c types biginteger long-integer

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

从函数返回字符串文字真的安全吗?

考虑这段代码:

char *test() {
    return "HELLO";
}

int main() {
    char *p = test();
    printf("%s\n", p);
}
Run Code Online (Sandbox Code Playgroud)

这会在没有警告的情况下进行编译,我猜是因为"HELLO"它没有存储在堆栈中。但这给了我一个警告:

char *test() {
    char arr[] = "HELLO";
    return arr;
}

int main() {
    char *p = test();
    printf("%s\n", p);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 字符串文字真的存储在称为字符串文字池的区域中吗?

  2. 如果是这样,存储在字符串文字池中的数据是否可以被视为全局数据?

  3. 从函数返回字符串文字总是安全的(因为它是全局的)?

c c-strings lifetime string-literals

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

C 中的指针及其大小

我已经尝试解决这个问题有一段时间了,并搜索了相关问题。我找到了一些有意义的答案,但显然我需要这个刻在石头上的答案。

目前我正在学习产品的一些固件。一组指针已初始化为uint8_t,如下例所示:

uint8_t *pwhatevs; 
Run Code Online (Sandbox Code Playgroud)

上面sizeof将返回1(一个字节)。当稍后在固件中分配该指针时,它将通过以下方式指向 32 位地址:

pwhatevs = (uint8_t *)&whatever; 
Run Code Online (Sandbox Code Playgroud)

这种语法对我来说似乎很奇怪。初始部分(uint8_t *)是 8 位指针类型转换。但如果我采取sizeof pwhatevsnow 它会返回4(4 个字节),我对此并不感到惊讶,但由于指针类型转换,我再次感到非常惊讶。有人可以解释一下吗?如果需要更多信息,我会尽可能提供!

编辑:在另一个与指针大小相关的问题中,提到指针的大小取决于系统,例如 16、32 或 64 位。这确实有道理,但是为什么*pwhatevs初始化时的大小不是 4 个字节呢?

编辑2:我无法分享整个代码,因为它是一个实际的产品,但会按照以下方式进行。如果这还不够,我将不得不研究一个具体的例子。

代码

从代码中分配大小的变量

c

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

为什么这个 C 程序打印 6?

我不明白为什么它打印6而不打印-8。如果我理解正确的话,x % 2等于所以0我必须寻找这种情况,0对吗?

#include <stdio.h>
#include <math.h>

int main(int argc, char *argv[]) {

    int x = 2;
    int a = 1;

    switch (x % 2 == 0) {
        case 3:
            a=a+3;
        case 0:
            a=a-4;
        case 6:
            a=a-5;
            break;
        case 7:
            a=a-2;
        case 1:
            a=a+2;
        case 4:
            a=a+3;
            break;
        case 9:
            a=a+4;
        case 8:
            a=a-2;
        case 5:
            a=a+3;
            break;
        case 2:
            a=a+1;
        default:
            a=a+4;
            break;
    }
    printf("%d\n", a);

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

c

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

相同的 C 代码,在 Mac 和 Linux 上的输出不同

我在运行下面的代码时弄乱了指针和const

int main()
{
    const int test = 10;
    int *ptr = &test;
    printf("%d\n", test);
    *ptr = 1;
    printf("%d\n", test);
}
Run Code Online (Sandbox Code Playgroud)

在 Mac 上,结果为:

10
10
Run Code Online (Sandbox Code Playgroud)

在linux机器上:

10
1
Run Code Online (Sandbox Code Playgroud)

为什么更改值在 Mac 上不起作用?两台机器都使用 gcc 编译代码。

c linux macos gcc undefined-behavior

0
推荐指数
2
解决办法
158
查看次数

返回非NULL字符串会导致编译错误

我正在尝试在C中做一些非常简单的事情,即处理并返回一个char,并使该函数的返回值成为另一个函数中的"可用"字符.

char name_the_file(char *fileName) {
    if (fileName == NULL) {
        fileName = "myFile";
    }
    globalFile = fileName;
    return fileName;
}

FILE *load_File(char *fileName) {
    fileName = name_the_file(fileName);
    FILE *filePointer;
    filePointer = open_file(fileName);
    return fopen(fileName, "r");
}
Run Code Online (Sandbox Code Playgroud)

我一直在接受诊断:warning: assignment makes pointer from integer without a cast [enabled by default]我不明白我在做什么有什么问题.我不能重新定义fileName

c

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

返回时double变为舍入

如何在没有舍入的情况下返回双精度数:

int function() {
    double d = 5.894; 
    printf("d=%f\n", d);
    return d;
}

int main() {
    double d = function();
    printf("d=%f\n", d);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这给了我

d=5.894000
d=5.000000
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

c double return return-value

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