相关疑难解决方法(0)

C中基本数据类型的大小

我有一个示例程序,我从一些网站复制.

int main(void)
{
   int answer;
   short x = 1;
   long y = 2;
   float u = 3.0;
   double v = 4.4;
   long double w = 5.54;
   char c = 'p';

   typedef enum
   {
      kAttributeInvalid,
      kBooleanAttributeActive,
      kBooleanAttributeAlarmSignal,
      kBooleanAttributeAlign64,
      kBooleanAttributeAutoNegotiationComplete,
   }codes_t;

  /* __DATE__, __TIME__, __FILE__, __LINE__ are predefined symbols */
  #if 0
  printf("Date : %s\n", __DATE__);
  printf("Time : %s\n", __TIME__);
  printf("File : %s\n", __FILE__);
  printf("Line : %d\n", __LINE__);
  #endif

  /* The size of various types */
  printf("The size of int         %zu\n", …
Run Code Online (Sandbox Code Playgroud)

c types

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

Free():无效的下一个尺寸(快速)错误

因此,我不断free(): invalid next size(fast)遇到此错误:运行代码时。如果我在函数末尾删除了free,我知道我正在泄漏内存,但是我不明白为什么会收到此错误。

我认为这与我错误地分配内存有关,但是我似乎找不到解决方法,这是我的代码:

bool parse(const char* line) //NOT WORKING JUST QUITE 
{
    char* copy = malloc(sizeof(line)); //allocate space for a copy of the line parameter
    strcpy(copy, line); //copy the line parameter

    char* method = strtok(copy, " "); //pointer to the method 
    char* reqLine = strtok(NULL, " "); //pointer to the requestline
    char* version = strtok(NULL, "\r\n"); //pointer to the HTTP-Version

    if (strcmp(method,"GET") != 0) //if the method is not GET
    {
        printf("%s\n", method);
        printf("ERROR 405\n");
        return …
Run Code Online (Sandbox Code Playgroud)

c string malloc free pointers

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

字符串末尾的空字符 '\0'

我有以下代码。

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

#define MAXLINE 1000

int main()
{
    int i;
    char s[MAXLINE];

    for (i = 0; i < 20; ++i) {
        s[i] = i + 'A';
        printf("%c", s[i]);
    }

    printf("\nstrlen = %d\n", strlen(s)); // strlen(s): 20
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我应该写吗

s[i] = '\0';
Run Code Online (Sandbox Code Playgroud)

在循环执行后显式标记字符串的结尾还是自动完成?没有s[i] = '\0';函数 strlen(s) 返回正确的值 20。

c string null c-strings null-terminated

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

这段代码的“假”输出背后的原因是什么?

此 C 代码给出输出“False”并且else块正在执行。

的值为sizeof(int)4,但 的值为sizeof(int) > -10。

我不明白发生了什么。

#include <stdio.h>
void main()
{
    if (sizeof(int) > -1 )
    {
       printf("True");
    }
    else
    {
        printf("False");
    }
    printf("\n%d", (sizeof(int)) ); //output: 4
    printf("\n%d", (sizeof(int) > -1) ); //output: 0
}
Run Code Online (Sandbox Code Playgroud)

c comparison unsigned gcc sizeof

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

为什么字符上的“~”运算符会产生更多字节?

我对以下代码感到困惑:

char c = 0x66;
printf("~c = %x\n", ~c); //0xffffff99
printf("size of c: %d\n", sizeof(c)); //1 byte
printf("Size of ~c: %d\n", sizeof(~c)); //4 bytes
Run Code Online (Sandbox Code Playgroud)

Achar只有 1 个字节,但使用~运算符的结果将是 4 个字节。为什么是~c 0xffffff99而不是的结果0x99

我犯了一些错误吗?

c

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

为什么(3&0x1111)= 1?

我不明白为什么3&0x1111 = 1?看起来:

对于任何无符号的32位整数i,i和0x1111应该是i,对吗?

但是当我在ubuntu 14.04上尝试这个时,我得到了3 & 0x1111=1.为什么?

int main() {
  unsigned int a =3;
  printf("size of a= %lu\n",sizeof(a));
  printf("value of 3 & 0x1111= %d\n",a & 0x1111);

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

c bit-manipulation

3
推荐指数
2
解决办法
585
查看次数

Understanding padding in structure

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)

c

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

当我使用“\x00”来中断字符串时,C 中的字符串长度是多少?

char buf1[1024] = "771675175\\x00AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";\nchar buf2[1024] = "771675175\\x00";\nchar buf3[1024] = "771675175\\0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";\nchar buf4[1024] = "771675175\\0";\nchar buf5[1024] = "771675175";\nbuf5[9] = 0;\nchar buf6[1024] = "771675175";\nbuf6[9] = 0;\nbuf6[10] = "A";\n\nprintf("%d\\n", strlen(buf1));\nprintf("%d\\n", strlen(buf2));\nprintf("%d\\n", strlen(buf3));\nprintf("%d\\n", strlen(buf4));\nprintf("%d\\n", strlen(buf5));\nprintf("%d\\n", strlen(buf6));\n\nif("\\0" == "\\x00"){\n    printf("YES!");\n}\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
10\n9\n9\n9\n9\n9\nYES!\n
Run Code Online (Sandbox Code Playgroud)\n

如上所示,我使用 来"\\x00"中断字符串。\n据我所知,当 strlen() 遇到"\\x00"\xef\xbc\x8c 时,它将返回终止符之前的字符数,并且不包括"\\x00".\ n但是这里,为什么buf1的长度等于10呢?

\n

c

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

可以#define用于替换c中的类型声明吗?

我遇到了一个问题,其中#define用于替换程序中的'int',如下所示

#define type int
int main()
{
 type *a,b;
}
Run Code Online (Sandbox Code Playgroud)

这是有效的吗?虽然它在我试图打印变量b的大小时给了我错误,说b是未声明的.我想知道这背后的具体原因.请告诉我.

有些用户告诉我,我隐藏了部分代码.我没有在上面的代码片段中给出printf语句.下面是printf的代码和给出错误的代码

#define type int;
int main()
{
 type* a, b;
 printf("%d",sizeof(b));
}
Run Code Online (Sandbox Code Playgroud)

c c-preprocessor

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

qsort与比较无符号int为32位,失败,我不看错误?

#define u32 uint32_t

struct edge {
 u32 v1;
 u32 v2;
};

struct edge *array = malloc((sizeof(struct edge))*Narray);

int struct_cmp_by_v1(const void *i, const void *j) {

  struct edge *a = (struct edge *)i;
  struct edge *b = (struct edge *)j;
  u32 x = a->v1;
  u32 y = b->v1;
  if(x < y)
    return -1;
  else if(x == y)
    return 0;
  else
    return 1;
  }

qsort(array, (size_t)(sizeof(array)/sizeof(struct edge)), sizeof(struct  edge), struct_cmp_by_v1);
Run Code Online (Sandbox Code Playgroud)

我想安排一个struct和qsort数组应该只比较结构的一个字段,以便结构被排序保持v1具有其对应的伙伴v2的不变量,即使在被v1排序后,情况是编译虽然它的顺序是-O3标志,但是输出在某种程度上是无序的,因为许多数量的u32可以重复,而有些很多很少,无论如何都没有顺序.

qsort命令,但它使它凌乱,在执行结束时,我要求一个函数来检查订单并退出:

amount of elements in the array:25056012
begins to order
finished …
Run Code Online (Sandbox Code Playgroud)

c

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