Home > OS >  Print a string from a pointer in C
Print a string from a pointer in C

Time:01-03

I have a dynamically sized CHARS array, I'm trying to print the string, using pointers. I tried something like this:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(){
    char *str = NULL;
    int size = 0;
    int c, i;

    printf("Please enter your command\n");
    while((c = getchar()) != EOF)
        {
            str = (char *)realloc(str, size   1);  
            str[size  ] = c; 
        }
    str = (char *)realloc(str, size   1);  
    str[size] = '\0';  
    free(str);
    
    printf("\nThe size is %d\n", size);

    /* here I want to print the string */
    for(i = *str; i!='\0'; i  ){
        printf("%c", *str i);
    }
    return 0;
}

This code does not print the string. The topic of using pointers is not really clear to me, so I think there is my mistake.

CodePudding user response:

You deallocated early allocated memory

free(str);

So after this statement the pointer str is invalid and accessing memory using the pointer invokes undefined behavior. You need to free the memory when the allocated character array will not be required any more.

This for loop

/* here I want to print the string */
for(i = *str; i!='\0'; i  ){
    printf("%c", *str i);
}

does not make sense. In this for loop the variable i that stores the code of the first character of the string stored in the dynamically allocated array is incremented. For example if the first character of the string is 'A' that is in ASCII 65 then after the first iteration it will be equal to 66 that corresponds to the character 'B'. And the expression *str i will look like 'A' 'B' that is the same as 65 66.

If you are going to outputted the stored string using a pointer then you should write for example

/* here I want to print the string */
for ( const char *p = str; *p != '\0';   p ){
    printf("%c", *p);
}
putchar( '\n' );

CodePudding user response:

You need to free the memory when you do not need it anymore.

Move free(str) to the end of main.

Also printf("%c", *str i); is wrong. You need to printf("%c", *(str i));

You do not check the result of realloc.

           char *tmp;
            tmp = realloc(str, size   1);  
            if(tmp)
            { 
                str = tmp;  
                str[size  ] = c; 
            }
            else
            { 
               /* handle error */ 
            }

more issues so I will post the working code:

int main(void){
    char *str = NULL;
    size_t size = 0;
    int c;
    size_t i;

    printf("Please enter your command\n");
    while((c = getchar()) != EOF)
        {
            char *tmp;
            tmp = realloc(str, size   2);  
            if(tmp)
            { 
                str = tmp;
                str[size  ] = c; 
            }
            else
            {
                return 1;
            }
        }
    str[size] = '\0';  
    
    printf("\nThe size is %zu\n", size);

    /* here I want to print the string */
    for(i = 0; str[i]; i  ){
        printf("%c", *(str i));
    }
    free(str);
    return 0;
}
  • Related