小编Bor*_*ito的帖子

是否在typedef声明中忽略了__attribute __((__ package ___))?

虽然__attribute__ ((aligned))适用于typedef声明,例如:

typedef struct __attribute__((__aligned__(8))) A {

     xxx ip    ;
     xxx udp   ;
     xxx ports ;
} table ;
Run Code Online (Sandbox Code Playgroud)

我遇到过声明,说__attribute__ ((__packed__)) 使用typedef 不是这种情况!我正在阅读一些相关的问题,其中一些使用了带有typedef的packed属性,它与我们的代码相符.

现在,在我们的代码中我们定义

typedef struct {
     xxx ip    ;
     xxx udp   ;
     xxx ports ;
}__attribute__((packed)) table ;
Run Code Online (Sandbox Code Playgroud)

上述声明是否使编译器以静默方式转储打包属性声明?

PS:是的,我本可以验证一下,但我现在的情况有所不同.让我们说假期和智能手机!

c attributes typedef data-structures

17
推荐指数
3
解决办法
3万
查看次数

指针初始化为什么?

有一件事总让我困惑,角色指针.经过四年多的努力,我再次陷入困境.

以上面提到的情况为例.为什么char指针会以这种方式运行?当指针指向什么都没有时,我们怎么能直接解决指针的内容呢?或者它就像char指针存储除地址之外的东西!

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

int main()
{ 
char* charPtr="I cant understand why";
int* intPtr=60;


printf("%d\n", intPtr); //displays 60
printf("%p\n", intPtr); // displays the hex value of 60

printf("%s\n", charPtr); // displays the wh0le string
printf("%p\n", charPtr); // displays the start address of the string
return 0;
Run Code Online (Sandbox Code Playgroud)

}

接下来是int指针,它如何接受值60以及它存储在何处?

抛开char指针和malloc,我认为指针的基本思想是得到一个指向的地址!

为什么这些案件

 *intptr = 60 ; // should be setting the pointee's value to 60
  intptr = 60 ;  // sets the address
Run Code Online (Sandbox Code Playgroud)

抛出编译错误

  int* intPtr=60; …
Run Code Online (Sandbox Code Playgroud)

c pointers function-pointers character

6
推荐指数
2
解决办法
511
查看次数

为什么strcpy会触发错误?

如果没有使用复制字符串,该程序工作正常strcpy,但我想知道原因?

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

int main ()
{

    int mat;
    char test1[20]={"Hex"} ;
    char test2[20]={"agonal"} ;

    strcpy(test1,"Diagonal"); // the print outputs the concatenated test if strcpy is commented out

    //printf("a=%c\nb=%c\n",test1[0],test1[1]);

    printf("Concatenated test=%s", strcat(test1, test2));

    return 0;
}


ERROR MESSAGE

*** buffer overflow detected ***: ./prog terminated
      ======= Backtrace: =========
      /lib/libc.so.6(__fortify_fail+0x48)[0xb75b6ae8]
      /lib/libc.so.6[0xb75b4b30]
      /lib/libc.so.6[0xb75b3dcd]
     ./prog(__gxx_personality_v0+0x14d)[0x804858d]
     ./prog(__gxx_personality_v0+0x31)[0x8048471]
      ======= Memory map: ========
Run Code Online (Sandbox Code Playgroud)

c

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