【问题标题】:initialize the struct pointer初始化结构指针
【发布时间】:2013-08-05 09:02:58
【问题描述】:
typedef struct
{
  char *s;
  char d;
}EXE;
EXE  *p;

对于上面的struct,如何用指针初始化结构?我知道我们做的非指针 EXE a[] = { {"abc",1}, {"def",2} }; 。同样,分配内存后是否可以使用指针?像p[] = { {"abc",1},.. so on} 这样说。基本上我想动态初始化。谢谢。

【问题讨论】:

  • 拿一本好书,以后避免问这样的问题
  • 我遗漏了什么,您能指点一下吗?这是基本/新手级别的问题吗?谢谢。
  • 看看我更新的答案。无论如何,我建议您阅读有关pointers 的内容,因为您似乎还不了解指针的工作原理。这很困难(指针),但值得花几个小时阅读它们。希望你会成功;)
  • 和问题的“水平”没有区别。

标签: c struct initialization


【解决方案1】:

我们可以用下面的指针初始化结构体

example:
 int i;
 char e[5]="abcd";
 EXE *p=malloc(sizeof(*p));
 for(i = 0;i < 5;i++)
   *(p+i)=(EXE){e,i+48};

【讨论】:

  • 感谢您的回复。为什么要将 i 增加到 48
  • 好的。知道了。您已尝试分配一些值。但这只会在分配的内存中存储 abcd 字符串。如果我想要不同的字符串和值怎么办。再次感谢。
  • 结构的第二个元素是字符右,所以加上ASCII值0 = 48,所以我们有字符0,1,2,3,4
  • 您为结构的第一个成员声明字符指针数组,为第二个成员声明整数数组并动态给出值。字符 *e[5];字符 [10]; for(j=0;j
  • 我在想,我知道有时你应该用 memset 初始化结构以填充 0,结构指针也应该这样做吗?不管是什么原因,请解释原因。我个人认为不应该,但我需要保证。
【解决方案2】:

首先,您需要为 char * 分配一些内存,然后使用 strcpy 库函数复制结构元素的数据。

p->s = strcpy(s,str);  //where str source, from where you need to copy the data

我希望这会有所帮助。虽然我可以给你完整的代码,但是我希望你尝试一下。

你可以用这个 Dynamically allocate C struct?

这是一个重复的问题。

【讨论】:

  • 感谢您的回复。我没有要求动态分配结构。我想像示例中提到的那样初始化分配的结构。
  • @foo_l... 我希望您应该为此编写代码。据我了解,我是对的......再给你一个例子stackoverflow.com/questions/6296880/…只需搜索你就会得到数百个这样的问题。
【解决方案3】:

你必须了解分配指针是如何工作的:

  1. 假设您已为三个结构 Ptr = malloc(3*sizeof(EXE)) 分配内存。
  2. 然后,当您将 1 添加到 Ptr 时,它会进入下一个结构。您有一块内存除以 3(每个结构有 3 个较小的内存块)。
  3. 因此,需要访问第一个结构的元素,然后将指针移动到下一个。

在这里你可以了解它是如何工作的:

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

typedef struct {
    char *s;
    char d;
} EXE;

int main()
{
    int i;
    EXE *Ptr;

    Ptr = malloc(3*sizeof(EXE)); // dymnamically allocating the
                                 // memory for three structures
    Ptr->s = "ABC";
    Ptr->d = 'a';

//2nd
    Ptr++;     // moving to the 2nd structure
    Ptr->s = "DEF";
    Ptr->d = 'd';

//3rd
    Ptr++;    // moving to the 3rd structure
    Ptr->s = "XYZ";
    Ptr->d = 'x';

//reset the pointer `Ptr`
    Ptr -= 2; // going to the 1st structure

//printing the 1st, the 2nd and the 3rd structs
    for (i = 0; i < 3; i++) {
        printf("%s\n", Ptr->s);
        printf("%c\n\n", Ptr->d);
        Ptr++;
    }   

    return 0;
}

注意: - 如果您有一个结构变量,请使用. 运算符来访问元素。 - 如果您有指向结构的指针,请使用 -&gt; 运算符来访问元素。

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

     struct EXE {
         int a;
     };

     int main(){

    struct EXE variable;
    struct EXE *pointer;

    pointer = malloc(sizeof(struct EXE)); // allocating mamory dynamically 
                                      // and making pointer to point to this
                                      // dynamically allocated block of memory
    // like here
    variable.a = 100;
    pointer->a = 100;

    printf("%d\n%d\n", variable.a, pointer->a);

    return 0;
    }

【讨论】:

  • 我知道这些基础知识。看来你没看懂我的问题。或者可能是我解释不清楚。似乎 ReDEyE 理解并且足够接近。无论如何,谢谢你的帖子。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
相关资源
最近更新 更多