C 结构 struct 指针和函数
C structure struct pointer and functions
最近看了一段代码,但对代码有疑问
toTs *now = try();
now->index = 30;
其中 toTs is a struct
和 try()
是 return 类型的函数 toTs*
*now
作为指针可以保留标签 try()
的地址但是因为 try()
不是结构变量 now
不能像结构一样访问它并且永远不能像 now->index=30
.
一样访问它
编译后显示segmentation fault
我只想问一下上面的代码合法不合法
#include <stdio.h>
#include <unistd.h>
typedef struct toT {
int index;
} toTs;
toTs lst[3];
toTs *try() {
int i;
for (i = 0; i < 3; i++) {
toTs *current = &lst[i];
printf("%d\n", current->index);
if (current->index == 3) {
printf("test work");
return current;
}
}
}
int main() {
int i;
for (i = 0; i < 3; i++) {
lst[i].index = i;
}
for (i = 0; i < 3; i++) {
printf("test %d\n", lst[i].index);
}
toTs *now = try();
now->index = 30;
printf("current %d\n", now->index);
printf("current %d\n", lst[2].index);
}
now 是一个可以指向结构变量的结构指针,但 try() 不是结构变量,也不是数据结构数组,它是一个函数
您需要始终 return 来自 try()
的有效 toTs*
才能使代码正常工作。
#include <stdio.h>
#include <unistd.h>
typedef struct toT {
int index;
} toTs;
toTs lst[4];
toTs *try() {
int i;
for (i = 0; i < 3; i++) {
toTs *current = &lst[i];
printf("%d\n", current->index);
if (current->index == 3) {
printf("test work");
return current;
}
}
return &lst[3]; // return a spare structure.
}
int main() {
int i;
for (i = 0; i < 3; i++) {
lst[i].index = i;
}
for (i = 0; i < 3; i++) {
printf("test %d\n", lst[i].index);
}
toTs *now = try();
now->index = 30;
printf("current %d\n", now->index);
for (i = 0; i < 4; i++) {
printf("Final %d\n", lst[i].index);
}
}
您将函数指针与 C 中的普通指针混淆了。函数指针更深入:https://en.wikipedia.org/wiki/Function_pointer
这段代码不是特别健康。
鉴于 lst
的初始化方式,当您调用 try()
时,它会在没有 return 语句的情况下到达该函数的末尾。
C 标准规定,如果您使用函数的 return 值,但实际上没有 return 值,则为未定义行为。
最近看了一段代码,但对代码有疑问
toTs *now = try();
now->index = 30;
其中 toTs is a struct
和 try()
是 return 类型的函数 toTs*
*now
作为指针可以保留标签 try()
的地址但是因为 try()
不是结构变量 now
不能像结构一样访问它并且永远不能像 now->index=30
.
编译后显示segmentation fault
我只想问一下上面的代码合法不合法
#include <stdio.h>
#include <unistd.h>
typedef struct toT {
int index;
} toTs;
toTs lst[3];
toTs *try() {
int i;
for (i = 0; i < 3; i++) {
toTs *current = &lst[i];
printf("%d\n", current->index);
if (current->index == 3) {
printf("test work");
return current;
}
}
}
int main() {
int i;
for (i = 0; i < 3; i++) {
lst[i].index = i;
}
for (i = 0; i < 3; i++) {
printf("test %d\n", lst[i].index);
}
toTs *now = try();
now->index = 30;
printf("current %d\n", now->index);
printf("current %d\n", lst[2].index);
}
now 是一个可以指向结构变量的结构指针,但 try() 不是结构变量,也不是数据结构数组,它是一个函数
您需要始终 return 来自 try()
的有效 toTs*
才能使代码正常工作。
#include <stdio.h>
#include <unistd.h>
typedef struct toT {
int index;
} toTs;
toTs lst[4];
toTs *try() {
int i;
for (i = 0; i < 3; i++) {
toTs *current = &lst[i];
printf("%d\n", current->index);
if (current->index == 3) {
printf("test work");
return current;
}
}
return &lst[3]; // return a spare structure.
}
int main() {
int i;
for (i = 0; i < 3; i++) {
lst[i].index = i;
}
for (i = 0; i < 3; i++) {
printf("test %d\n", lst[i].index);
}
toTs *now = try();
now->index = 30;
printf("current %d\n", now->index);
for (i = 0; i < 4; i++) {
printf("Final %d\n", lst[i].index);
}
}
您将函数指针与 C 中的普通指针混淆了。函数指针更深入:https://en.wikipedia.org/wiki/Function_pointer
这段代码不是特别健康。
鉴于 lst
的初始化方式,当您调用 try()
时,它会在没有 return 语句的情况下到达该函数的末尾。
C 标准规定,如果您使用函数的 return 值,但实际上没有 return 值,则为未定义行为。