简体   繁体   中英

print out a string in C

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:

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++;
}

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

please help! thanks in advance

-- added--- this is code for 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;

}

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

You are returning centerString from center_string . However, centerString is declared as an array on the stack. So when center_string returns, the storage for centerString is no longer available.

You have a couple of options:

  1. You can allocated centerString on 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_string or new_center_string .

  2. You can declare centerString as static :

     static char centerString[34]; 

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

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

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

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

void center_string(char *msg, char *centerString)

and declare centerString in same method you call center_string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM