将指针传递给函数并在被调用函数内分配空间。在C中可能吗?
我需要传递某种引用指针(在调用函数时还没有为它分配空间)。我想在被调用函数中调用malloc。指针(如将指针传递给函数并在被调用函数内分配空间。在C中可能吗?,c,C,我需要传递某种引用指针(在调用函数时还没有为它分配空间)。我想在被调用函数中调用malloc。指针(如(指针+索引)=malloc(…))可以指向任意数量的对象,以在被调用函数中分配空间。在调用函数中,我还需要能够访问和修改对象,如(指针+索引)->member=some_val或pointer[index]->member=some_val。这可能吗 这就是我所尝试的: struct abc{ char *c;}; void abc(struct abc *t); void main() {
(指针+索引)=malloc(…)
)可以指向任意数量的对象,以在被调用函数中分配空间。在调用函数中,我还需要能够访问和修改对象,如(指针+索引)->member=some_val
或pointer[index]->member=some_val
。这可能吗
这就是我所尝试的:
struct abc{ char *c;};
void abc(struct abc *t);
void main() {
struct abc *k;
printf("%s",k->c);
}
void abc(struct abc *t){
t=(struct abc *)malloc(sizeof(struct abc));
*t->c="hello";
}
从注释继续,最初的问题是您正在按值将指针传递给函数
abc()
。函数将收到指针的副本,当函数返回时,您在函数abc()
中对指针副本所做的任何更改都将丢失。(您还错误地取消了对*t->c=“hello”
的引用)
您有两个视区:
传递指针的地址
当您将指针的地址传递给函数时,您将从main()
向函数传递一个指向指针的指针,该指针包含指针的原始地址。然后,您可以通过在abc()
函数中更新原始指针持有的地址来更改原始指针持有的地址。例如:
#include <stdio.h>
#include <stdlib.h>
struct abc {
char *c;
};
void abc (struct abc **t) /* use pointer-to-poitner to struct abc */
{
*t = malloc (sizeof **t); /* allocate */
if (*t == NULL) { /* validate */
perror ("malloc-*t");
return;
}
(*t)->c = "hello"; /* assign on success */
}
int main (void) { /* void main only valid in freestanding environment */
struct abc *k;
abc (&k); /* call abc passing address of k */
if (k) { /* validate k not NULL */
puts (k->c);
free (k); /* don't forget to free what you allocate */
}
}
将函数返回类型更改为struct abc*
并返回分配的指针
第二个选项是简单地更改abc()
函数的返回类型,以返回指向struct abc
(例如struct abc*
)的指针。进行更改时,不需要向函数传递任何参数。只需将指针声明为函数的本地指针,并为一个struct abc
分配存储,将分配的块的地址分配给本地指针,然后将该指针返回给调用方(main()
此处)
例如,您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
struct abc {
char *c;
};
struct abc *abc (void) /* return pointer to allocated struct */
{
struct abc *t = malloc (sizeof *t); /* allocate */
if (t == NULL) { /* validate */
perror ("malloc-t");
return NULL;
}
t->c = "hello"; /* assign on success */
return t; /* return pointer to allocated struct */
}
int main (void) { /* void main only valid in freestanding environment */
struct abc *k = abc(); /* declare and initialize k */
if (k) { /* validate k not NULL */
puts (k->c);
free (k); /* don't forget to free what you allocate */
}
}
#包括
#包括
结构abc{
char*c;
};
结构abc*abc(void)/*返回指向已分配结构的指针*/
{
结构abc*t=malloc(sizeof*t);/*allocate*/
如果(t==NULL){/*validate*/
perror(“malloc-t”);
返回NULL;
}
t->c=“hello”/*成功分配*/
返回t;/*返回指向已分配结构的指针*/
}
int main(void){/*void main仅在独立环境中有效*/
结构abc*k=abc();/*声明并初始化k*/
如果(k){/*验证k不为空*/
put(k->c);
免费(k);/*别忘了免费分配你分配的东西*/
}
}
别忘了验证退货
(输出相同)
除非在独立环境中,否则main()
的正确声明是int main()
除非您是在独立环境(没有任何操作系统的好处)中编程,否则在符合标准的实现中,main
的允许声明是int main(void)
和int main(int argc,char*argv[])
(您将看到使用等效的char**argv
编写)。请参阅:。另见:
在独立环境中,程序启动时调用的函数的名称和类型由实现定义。见:
请仔细查看,如果您还有其他问题,请告诉我。继续评论,您最初的问题是您正在通过值将指针传递给函数
abc()
。函数将收到指针的副本,当函数返回时,您在函数abc()
中对指针副本所做的任何更改都将丢失。(您还错误地取消了对*t->c=“hello”
的引用)
您有两个视区:
传递指针的地址
当您将指针的地址传递给函数时,您将从main()
向函数传递一个指向指针的指针,该指针包含指针的原始地址。然后,您可以通过在abc()
函数中更新原始指针持有的地址来更改原始指针持有的地址。例如:
#include <stdio.h>
#include <stdlib.h>
struct abc {
char *c;
};
void abc (struct abc **t) /* use pointer-to-poitner to struct abc */
{
*t = malloc (sizeof **t); /* allocate */
if (*t == NULL) { /* validate */
perror ("malloc-*t");
return;
}
(*t)->c = "hello"; /* assign on success */
}
int main (void) { /* void main only valid in freestanding environment */
struct abc *k;
abc (&k); /* call abc passing address of k */
if (k) { /* validate k not NULL */
puts (k->c);
free (k); /* don't forget to free what you allocate */
}
}
将函数返回类型更改为struct abc*
并返回分配的指针
第二个选项是简单地更改abc()
函数的返回类型,以返回指向struct abc
(例如struct abc*
)的指针。进行更改时,不需要向函数传递任何参数。只需将指针声明为函数的本地指针,并为一个struct abc
分配存储,将分配的块的地址分配给本地指针,然后将该指针返回给调用方(main()
此处)
例如,您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
struct abc {
char *c;
};
struct abc *abc (void) /* return pointer to allocated struct */
{
struct abc *t = malloc (sizeof *t); /* allocate */
if (t == NULL) { /* validate */
perror ("malloc-t");
return NULL;
}
t->c = "hello"; /* assign on success */
return t; /* return pointer to allocated struct */
}
int main (void) { /* void main only valid in freestanding environment */
struct abc *k = abc(); /* declare and initialize k */
if (k) { /* validate k not NULL */
puts (k->c);
free (k); /* don't forget to free what you allocate */
}
}
#包括
#包括
结构abc{
char*c;
};
结构abc*abc(void)/*返回指向已分配结构的指针*/
{
结构abc*t=malloc(sizeof*t);/*allocate*/
如果(t==NULL){/*validate*/
perror(“malloc-t”);
返回NULL;
}
t->c=“hello”/*成功分配*/
返回t;/*返回指向已分配结构的指针*/
}
int main(void){/*void main仅在独立环境中有效*/
结构abc*k=abc();/*声明并初始化k*/
如果(k){/*验证k不为空*/
put(k->c);
免费的(k);/*别忘了免费你要的