C语言 在 C 中打印出一个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4776274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:35:50  来源:igfitidea点击:

print out a string in C

cstring

提问by Tri

I'm at beginner level in C programming language. I'm trying to print out a string which its pointer is passed from a function to another pointer variable in main() function. My knowledge is very limited in C and I have looked up on the web (probably I have not look deep enough but please help) here is the code:

我是 C 编程语言的初学者。我试图打印出一个字符串,它的指针从一个函数传递到 main() 函数中的另一个指针变量。我在 C 方面的知识非常有限,我在网上查过(可能我看的不够深入,但请帮助)这里是代码:

int i;
char *result;
result= center_string("I go to school everyday\nto study");

i=0;
while (result[i]!='
static char* center_string(char* msg){
char toReturn[34]; // the return message has to contain two lines in one. It might be better to form this string while traversing two strings, rather than forming by the union of two lines
char centerString[34];

// padding can be done during the process of splitting two lines or after.
// it's harder to do while splitting two lines because the length of each line is unknown
// countSecond starts with -1 because it contains \n which should not be in it. So this number will be discarded
int paddingFirst = 0, paddingSecond = 0, countFirst = 0, countSecond = -1, countReturn = 0; // maximum number of return string is 34

// stillOnTheFirstLine is a boolean variable but representing by a integer with 1 digit
int i = 0, stillOnTheFirstLine = 0; // start counting the character in the second line 

 //initialize the return line
for (i=0; i<34; i++){
    toReturn[i] = '
static char centerString[34];
'; centerString[i] = '
static char centerString[34];
'; // printf("initializing toReturn[%i] with value %c\n",i, toReturn[i]); } i = 0; while ( msg[i]!='##代码##' ) { // getting the first line to display // the limit is 16 chars or \n to the second line // i.e: I go to school e|very day\nto study // firstLine = "I go to school e" // secondLine ="to study" // discarded = "very day" // get the first line if (i<=15 && msg[i]!='\n'){ // this is the first line // insert the character into the return string toReturn[countReturn] = msg[i]; //printf("First line: %c\n", toReturn[countReturn]); countReturn++; countFirst++; }else{ // when the first line is finished // there are two cases // first case: when the first line is longer than 16 character long if (msg[i]!='\n' && i>15 && stillOnTheFirstLine==0){ // discard the character //printf("Discard: %c\n", msg[i]); }else{ stillOnTheFirstLine = 1; // yes the parser is still on the second line // this is the second case which the first line is finished // check for 16 character instead of 15 because one of them is \n which separate line 1 and 2 if (countReturn<33 && stillOnTheFirstLine==1){ toReturn[countReturn] = msg[i]; // put the character from the msg string to the toReturn //printf("Second line: %c\n", toReturn[countReturn]); countReturn++; countSecond++; } // whatever after this is discarded } } i++; } // start padding paddingFirst = floor((double)(16-countFirst)/2); paddingSecond = floor((double)(16-countSecond)/2); // padding the first line i=0; while (toReturn[i]!='##代码##' && toReturn[i]!='\n'){ centerString[paddingFirst] = toReturn[i]; paddingFirst++; i++; } // add '\n' into the centerString centerString[paddingFirst] = toReturn[i]; // then continue with the next character in toReturn i++; // padding the second line paddingSecond = i+paddingSecond; while(toReturn[i]!='##代码##'){ centerString[paddingSecond] = toReturn[i]; paddingSecond++; i++; } /*for(i=0; i<34; i++){ printf("the result string is: %c\n", centerString[i]); }*/ return centerString;
'){ printf("%c\n", result[i]); i++; }

and the result I received back are 4 weird characters arranged vertically (unfortunately stackoverflow.com does not let me post the picture)

我收到的结果是垂直排列的 4 个奇怪的字符(不幸的是 stackoverflow.com 不允许我发布图片)

please help! thanks in advance

请帮忙!提前致谢

-- added--- this is code for center_string:

-- 添加 --- 这是 center_string 的代码:

##代码##

}

}

采纳答案by stefan

You need to malloc the string you return or take a pointer as argument:

您需要 malloc 返回的字符串或将指针作为参数:

void center_string(char *msg, char *centerString)

void center_string(char *msg, char *centerString)

and declare centerString in same method you call center_string.

并以与调用 center_string 相同的方法声明 centerString。

回答by Elalfer

Why can't you just do printf("%s\n", result)

为什么你不能做 printf("%s\n", result)

回答by Daniel Gallagher

You are returning centerStringfrom center_string. However, centerStringis declared as an array on the stack. So when center_stringreturns, the storage for centerStringis no longer available.

centerString从回来center_string。但是,centerString在堆栈上被声明为一个数组。因此,当center_string返回时,存储centerString不再可用。

You have a couple of options:

你有几个选择:

  1. You can allocated centerStringon the heap (using malloc), but you should make that clear that the caller will need to free the storage by calling the function something like make_center_stringor new_center_string.

  2. You can declare centerStringas static:

    ##代码##

    This makes centerStringuse a different storage space than the stack (the same as global variables, but you can only access it directly from within the center_stringfunction; but center_stringcan return its address to another function). This will let you return centerStringcorrectly and use it in main.

  1. 您可以centerString在堆上分配(使用malloc),但您应该清楚地表明调用者需要通过调用类似make_center_string或的函数来释放存储空间new_center_string

  2. 您可以声明centerStringstatic

    ##代码##

    这使得centerString使用与堆栈不同的存储空间(与全局变量相同,但您只能从center_string函数内部直接访问它;但center_string可以将其地址返回给另一个函数)。这将使您centerString正确返回并在main.

回答by David Heffernan

You are returning a buffer allocated on the stack of center_string and that will no longer be valid once you leave that routine.

您正在返回一个分配在 center_string 堆栈上的缓冲区,一旦您离开该例程,该缓冲区将不再有效。

I'm sure there are other problems but I guess that's the main one right now!

我确定还有其他问题,但我想这是现​​在的主要问题!