我是C编程语言的初学者。我正在尝试打印出一个字符串,其指针从函数传递到main()函数中的另一个指针变量。我的知识在C中非常有限,我在网上查了一下(可能我看起来不够深,但请帮忙) 这是代码:
int i;
char *result;
result= center_string("I go to school everyday\nto study");
i=0;
while (result[i]!='\0'){
printf("%c\n", result[i]);
i++;
}
我收到的结果是4个垂直排列的奇怪字符(不幸的是stackoverflow.com不允许我发布图片)
请帮忙!提前谢谢
- 补充--- 这是center_string的代码:
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] = '\0';
centerString[i] = '\0';
// printf("initializing toReturn[%i] with value %c\n",i, toReturn[i]);
}
i = 0;
while ( msg[i]!='\0' ) {
// 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]!='\0' && 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]!='\0'){
centerString[paddingSecond] = toReturn[i];
paddingSecond++;
i++;
}
/*for(i=0; i<34; i++){
printf("the result string is: %c\n", centerString[i]);
}*/
return centerString;
}
答案 0 :(得分:2)
为什么你不能printf("%s\n", result)
答案 1 :(得分:2)
您将从centerString
返回center_string
。但是,centerString
被声明为堆栈上的数组。因此,当center_string
返回时,centerString
的存储空间不再可用。
您有几个选择:
您可以在堆上分配centerString
(使用malloc
),但是您应该明确调用者需要通过调用{{1}之类的函数来释放存储空间}或make_center_string
。
您可以将new_center_string
声明为centerString
:
static
这使得static char centerString[34];
使用与堆栈不同的存储空间(与全局变量相同,但您只能直接从centerString
函数中访问它;但center_string
可以返回它的地址到另一个功能)。这样您就可以正确地返回center_string
并在centerString
中使用它。
答案 2 :(得分:1)
您正在返回在center_string堆栈上分配的缓冲区,并且在您离开该例程后将不再有效。
我确定还有其他问题,但我猜这是现在的主要问题!
答案 3 :(得分:0)
您需要对返回的字符串进行malloc或将指针作为参数:
void center_string(char * msg,char * centerString)
并使用您调用center_string的相同方法声明centerString。