从函数返回char数组 [英] returning char arrays from a function

查看:114
本文介绍了从函数返回char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么可以返回指向字符串文字的指针

(即1)但不是已经显式分配的数组。 (即2)?

两者都将被分配到堆栈中,为什么第一个不会导致

编译器警告?


#include< stdio.h>


char * funca(){

char * a =" blah" ;; // 1 - ok

// char a [] =" blah" ;; // 2 - 不行

返回a;

}


int main(){

funca();

返回0;

}

I am wondering why it is possible to return a pointer to a string literal
(ie. 1) but not an array that has been explicitly allocated. (ie. 2) ?
Both would be allocated on the stack, why does the first one not cause a
compiler warning?

#include <stdio.h>

char * funca() {
char *a = "blah"; //1 - ok
// char a[] = "blah"; //2 - not ok
return a;
}

int main() {
funca();
return 0;
}

推荐答案

Robert Smith写道:
Robert Smith wrote:
我想知道为什么可以返回指向字符串文字的指针
(即1)但不是已经显式分配的数组。 (即2)?
两者都将被分配到堆栈中,


前向参考:

char * a =" blah" ;; // 1 - ok
// char a [] =" blah" ;; // 2 - 不行
I am wondering why it is possible to return a pointer to a string literal
(ie. 1) but not an array that has been explicitly allocated. (ie. 2) ?
Both would be allocated on the stack,
Forward reference:
char *a = "blah"; //1 - ok
// char a[] = "blah"; //2 - not ok




否:字符串文字进入静态存储并存在于程序'

的生命周期内。 />

-

Chris" xf == f(x)" Dollin

小心三月的想法。



No: string literals go into static store and exist for the program''s
lifetime.

--
Chris "x.f == f(x)" Dollin
Beware the ideas of March.




Robert Smith写道:

Robert Smith wrote:
我想知道为什么可以返回一个指向字符串文字的指针(即1),但不是一个已经显式分配的数组。 (即2)?
两者都会在堆栈上分配,为什么第一个不会导致
编译器警告?

#include< stdio.h>

char * funca(){
char * a =" blah" ;; // 1 - ok
// char a [] =" blah" ;; // 2 - 不行
返回一个;
}
int main(){
funca();
返回0;
}
I am wondering why it is possible to return a pointer to a string literal
(ie. 1) but not an array that has been explicitly allocated. (ie. 2) ?
Both would be allocated on the stack, why does the first one not cause a
compiler warning?

#include <stdio.h>

char * funca() {
char *a = "blah"; //1 - ok
// char a[] = "blah"; //2 - not ok
return a;
}

int main() {
funca();
return 0;
}




第一个a指向一个* const *字符串,它不会被分配在

堆栈上,它是'在数据段上。



the first a is pointed to a *const* string , it is not be allocated on
the stack, it''s on the DATA segment.


zh ***** @ gmail.com 写道:
zh*****@gmail.com wrote:
罗伯特·史密斯写道:
Robert Smith wrote:
我想知道为什么有可能返回指针字符串文字
(即1)但不是已明确分配的数组。 (即2)?
两者都会在堆栈上分配,为什么第一个不会导致
编译器警告?

#include< stdio.h>

char * funca(){
char * a =" blah" ;; // 1 - ok
// char a [] =" blah" ;; // 2 - 不行
返回一个;
}
int main(){
funca();
返回0;
第一个a指向一个* const *字符串,
I am wondering why it is possible to return a pointer to a string literal
(ie. 1) but not an array that has been explicitly allocated. (ie. 2) ?
Both would be allocated on the stack, why does the first one not cause a
compiler warning?

#include <stdio.h>

char * funca() {
char *a = "blah"; //1 - ok
// char a[] = "blah"; //2 - not ok
return a;
}

int main() {
funca();
return 0;
}
the first a is pointed to a *const* string ,




Nitpick:不,它不是。它指向一系列不可修改的字符,由于历史原因,

是_not_ const。

它不是在堆栈上分配的,它在DATA上段。



Nitpick: no, it isn''t. It points at an array of unmodifiable chars which
are, for historical reasons, _not_ const.
it is not be allocated on the stack, it''s on the DATA segment.




你不知道。你知道的是,无论它们在哪里,它们都会有静态的持续时间。给它们提供静态持续时间的一种可能方法是将它们放在一个框架中的堆栈中,该框架仅在程序退出之前释放




Richard



You don''t know that. What you do know is that, wherever they are, they
have static duration. One possible way to give them static duration is
to put them on the stack in a frame which gets released only just before
the program exits.

Richard


这篇关于从函数返回char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆