0

I want to create a string without knowing the exact dimension of it, is this correct or it will have unpredictable behavior?

char *p;
p="unknow string size";

If this is wrong, how i can create something similar, and modify it with string.h fucntion?

[edit]I read again the answer and it was not completly clear, my first doubt is: are those two codes equals?

char *p="unknow string size"

and

char *p;
p="unknow string size";
baboo
  • 35
  • 7
  • 1
    http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c probably helps... – mafso Jan 25 '15 at 16:45

3 Answers3

2

The only solution in C is to use realloc function

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

int main(void)
{
    char *s = malloc(1);
    printf("Enter a string: \t"); // It can be of any length
    int c;
    int i = 0;
    while((c = getchar()) != '\n' && c != EOF)
    {
        s[i++] = c;
        s = realloc(s, i+1);
    }
    s[i] = '\0';
    printf("Entered string: \t%s", s);
    free(s);
    return 0;
}  

are those two codes equals?

char *p="unknown string size"

and

char *p;
p="unknown string size";  

No. First snippet is declaring p as a pointer to char and initializing it to point to a string literal "unknown string size". In second snippet, p is defined as a pointer to char and then an assignment is done to it.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • That's one of the case i wanted to use it, create a dynamic string based on user imput. Thanks for this method. – baboo Jan 25 '15 at 16:57
  • 1
    This line: 'char *s = malloc(1);' 'should' be ' char *s = NULL;' the realloc() function handles a NULL pointer correctly. The code should be checking the returned value from realloc() to assure the operation was successful. The current code would result in a memory leak if realloc fails because 's' would receive NULL, thereby losing the pointer to the previously allocated memory. – user3629249 Jan 25 '15 at 17:13
  • @WhozCraig; No. I do not intended to include `\n`. – haccks Jan 25 '15 at 17:15
  • rather than the current loop, the code could use 'getline()' then trim off the trailing newline (by replacing the newline with a '\0' character) and then calling free() with the pointer returned by getline() – user3629249 Jan 25 '15 at 17:15
  • @user3629249; Yes you can use `NULL`, but in that case `s = realloc(s, i+1);` would be the first statement of the body. Thanks for suggesting `getline` but it is not supported in pure C. – haccks Jan 25 '15 at 17:17
0

It depends, you don't assign the value, y point to the address where "unknow string size" is stored, so you should know that for example you can't modify the string.

If you don't want to modify the string then it's ok, but you should also protect the pointer for trying to write to that memory location cause that would be undefined behavior, you can do this

const char *p;
p = "unknow string size";

this will not prevent you from modifying the string, but then you would need to explicitly cast the const qualifier away to do so.

If you intend to modify the string later, then this is not the way to do it, in that case you should do this

char *p;
size_t length;

length = strlen("unknow string size");
p = malloc(1 + length);
if (p == NULL)
    memoryAllocationProblemDoNotContinue();
strcpy(p, "unknow string size");
 .
 .
 .
 /* use p here */
 .
 .
 .
 free(p);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • can I first define the string uninitialized, than assign a value to it, and only after i assign the value it became read only? So, if i don't need to modify it later the code is correct right? – baboo Jan 25 '15 at 17:01
  • It is as long as you never try to access the pointer before initialization. – Iharob Al Asimi Jan 25 '15 at 17:06
0
I would write the function like this:

#include <stdio.h> // printf(), getline()
#include <stdlib.h>
#include <string.h> // strlen()

int main(void)
{
    char *s = NULL;

    printf("Enter a string: \t"); // It can be of any length
    fflush(stdout);

    getline(&s, 0, stdin); // note: first param is **char

    if( NULL != s )
    { // then getline successful

        // trim trailing newline
        if( '\n' == s[strlen(s)-1] ) s[strlen(s)-1] = '\0'; 

        printf("Entered string: \t%s", s);
        free(s);
    }  // end if
    return 0;
} // end function: main
user3629249
  • 16,402
  • 1
  • 16
  • 17