定义结构的程序局部指针
Defining a program local pointer of a structure
我的问题涉及创建在整个程序文件中可见的变量。换句话说,一个 file-local 变量。
考虑这个例子
#include <stdio.h>
struct foo
{
char s[] = "HELLO";
int n = 5;
};
struct foo *a;
int main()
{
puts("Dummy outputs!\n");
printf("%s\n",a->s);
printf("%d\n",a->n);
return 0;
}
现在,此代码段不会 运行。
为什么?
因为指向指针变量 a 的结构将不会 分配 因为该语句从未执行过。
现在,如何在不更改此变量范围的情况下分配它 a?
#include <stdio.h>
struct foo {
char const *s;
int n;
};
/* static for file-local */
static struct foo a = { "HELLO" , 5 };
int main(void) {
printf("%s\n", a.s);
printf("%d\n", a.n);
return 0;
}
Now, how do you get it allocated without changing the scope of this variable a?
我相信有很多方法可以解决您的问题。这是我的建议。
更改 struct foo
的定义以在 s
中包含固定数量的字符。
创建 a
作为对象而不是指针。用必要的值初始化它。
使 a
成为 static
变量,因此它的使用仅限于文件。
在文件的其余部分使用对象 a
而不是指针 a
。
#include <stdio.h>
struct foo
{
char s[20];
int n;
};
static struct foo a = {"HELLO", 20};
int main()
{
puts("Dummy outputs!\n");
printf("%s\n",a.s);
printf("%d\n",a.n);
return 0;
}
这个:
struct foo
{
char s[] = "HELLO";
int n = 5;
};
不是有效的 C 代码。您首先声明类型:
struct foo
{
char s[10];
int n;
};
然后定义一个该类型的变量:
static struct foo a = { "HELLO", 5 };
static
关键字允许此变量具有文件本地范围。
您现在可以像这样使用它:
static struct foo a = { "HELLO", 5 };
void other()
{
puts("Dummy outputs!\n");
printf("%s\n",a.s);
printf("%d\n",a.n);
}
int main()
{
puts("Dummy outputs!\n");
printf("%s\n",a.s);
printf("%d\n",a.n);
other();
return 0;
}
请注意,a
可从两个函数访问。但是,它不能从其他文件中定义的函数中查看,因为它被声明为 static
.
至于直接使用指针还是直接使用结构体,你可以在需要的时候随时获取这个结构体的地址,这样使用它:
some_function(&a);
well, i need to use a pointer instead of a structure directly
试试这个:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct foo{
char s[20];
int n;
};
int main(void){
struct foo *a;
a = malloc(sizeof(struct foo));
puts("Dummy outputs!\n");
strcpy(a->s, "HELLO");
a->n = 5;
printf("%s\n",a->s);
printf("%d\n",a->n);
free(a);
return 0;
}
输出:
Dummy outputs!
HELLO
5
我的问题涉及创建在整个程序文件中可见的变量。换句话说,一个 file-local 变量。
考虑这个例子
#include <stdio.h>
struct foo
{
char s[] = "HELLO";
int n = 5;
};
struct foo *a;
int main()
{
puts("Dummy outputs!\n");
printf("%s\n",a->s);
printf("%d\n",a->n);
return 0;
}
现在,此代码段不会 运行。
为什么?
因为指向指针变量 a 的结构将不会 分配 因为该语句从未执行过。
现在,如何在不更改此变量范围的情况下分配它 a?
#include <stdio.h>
struct foo {
char const *s;
int n;
};
/* static for file-local */
static struct foo a = { "HELLO" , 5 };
int main(void) {
printf("%s\n", a.s);
printf("%d\n", a.n);
return 0;
}
Now, how do you get it allocated without changing the scope of this variable a?
我相信有很多方法可以解决您的问题。这是我的建议。
更改
struct foo
的定义以在s
中包含固定数量的字符。创建
a
作为对象而不是指针。用必要的值初始化它。使
a
成为static
变量,因此它的使用仅限于文件。在文件的其余部分使用对象
a
而不是指针a
。
#include <stdio.h>
struct foo
{
char s[20];
int n;
};
static struct foo a = {"HELLO", 20};
int main()
{
puts("Dummy outputs!\n");
printf("%s\n",a.s);
printf("%d\n",a.n);
return 0;
}
这个:
struct foo
{
char s[] = "HELLO";
int n = 5;
};
不是有效的 C 代码。您首先声明类型:
struct foo
{
char s[10];
int n;
};
然后定义一个该类型的变量:
static struct foo a = { "HELLO", 5 };
static
关键字允许此变量具有文件本地范围。
您现在可以像这样使用它:
static struct foo a = { "HELLO", 5 };
void other()
{
puts("Dummy outputs!\n");
printf("%s\n",a.s);
printf("%d\n",a.n);
}
int main()
{
puts("Dummy outputs!\n");
printf("%s\n",a.s);
printf("%d\n",a.n);
other();
return 0;
}
请注意,a
可从两个函数访问。但是,它不能从其他文件中定义的函数中查看,因为它被声明为 static
.
至于直接使用指针还是直接使用结构体,你可以在需要的时候随时获取这个结构体的地址,这样使用它:
some_function(&a);
well, i need to use a pointer instead of a structure directly
试试这个:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct foo{
char s[20];
int n;
};
int main(void){
struct foo *a;
a = malloc(sizeof(struct foo));
puts("Dummy outputs!\n");
strcpy(a->s, "HELLO");
a->n = 5;
printf("%s\n",a->s);
printf("%d\n",a->n);
free(a);
return 0;
}
输出:
Dummy outputs! HELLO 5