Question:
Help printing out a string in C?
ML
2013-02-18 17:04:37 UTC
Hi, I'm trying to make a string in C and then print it out by just using pointers and no arrays. However, I get a 'segmentation fault: 11' whenever I run the code. Could someone help me figure out the problem? Thank you!

int main(){
int i, sizeString1;
char *s1;
printf("Input: \t");
sizeString1 = make_string(&s1);
printf("Output: \t, %s", s1);
free(s1);
return 0;
}

String *make_string(char *s1){
char *a, b;
int i, size=0;
String *new = (String *) malloc(sizeof(String));
//a = new->chars; <--- problem only created space for struct not the dynamic array
a = (char *) malloc(sizeof(char));
b = getchar();

while(b != '\n' && size < MAX){
*(a+size) = b;
size++;
b = getchar();
a = realloc(a, size+1);
}

*(a+size) = '\0'; //end of String marker
new->chars = a;
new->length = size;

*s1 = *a;

return new;
}

I defined a String as

typedef struct{
char *chars;
int length;
} String;
Three answers:
cja
2013-02-19 09:56:40 UTC
As the previous responder mentioned, make_string takes a char* arg, but you're passing a char**. Also, it returns a String* which you're storing to an int. There's no reason to limit string size to MAX, whatever you've defined that to be. The only thing you need to worry about is running out of dynamic memory, which is not likely. If you're worried about that, just make sure realloc doesn't return a null pointer.



This is an extremely inefficient way to get and store a string input by the user, but it can work. Try this:



#include

#include



typedef struct{

    char *chars;

    int length;

} String;



String *make_string(void);

void delete_string(String *);



int main(int argc, char *argv[]) {

    String *s1;



    while (1) {

        printf("\nInput: ");

        s1 = make_string();

        printf("Output: %s\n", s1->chars);

        delete_string(s1);

    }

    return 0;

}



String *make_string() {

    String *s1 = (String *)malloc(sizeof(String));



    s1->chars = (char *) malloc(sizeof(char));

    s1->length = 0;

    do {

        *(s1->chars + s1->length) = getchar();

        if (*(s1->chars + s1->length) != '\n') {

            s1->chars = realloc(s1->chars, ++s1->length + 1);

        }

    } while (*(s1->chars + s1->length) != '\n');

    *(s1->chars + s1->length) = '\0';

    return s1;

}



void delete_string(String *s1) {

    free(s1->chars);

    free(s1);

}



#if 0



Sample run:



Input: hello, world.

Output: hello, world.



Input: bye

Output: bye



#endif
2013-02-18 18:13:19 UTC
edit: OIC, your make_string reads in the string from input. I don't think that is good naming



There should be several ways to create the string from existing strings, or from char buffers.



To read from input, you should call it stringGetLine (String **strpp) stringGetWord (String**strpp) stringGetInput or something like that which would be more clear. You can use String *strp in all those depending how you defined your String data type

....................



I have no idea what you are doing. It looks totally screwed up. If you want to create a c data type

with a structure you are going all about it wrong. In C you create structure then you create functions to support your data type.



C++ took it another step further and made the functions part of the data type.





If you want to do this with C and structures and functions you could get started with the struct you have defined.



typedef struct{

char *chars;

int length;

} String;



String *CreateString ( char *st)

{

String *string = (String*) malloc (sizeof(String));

string->chars = (char*) malloc ( string -> length = ( strlen( st) +1) );

strcpy ( string -> chars, st );

return string;

}



void DisplayString ( String * st)

{

printf ("%s", st -> chars);

}



etc.



if you want to know, this part of your code should have produced and error during compilation



sizeString1 = make_string(&s1);



you defined make_string as taking a char*



s1 is a char*



&s1 is a char**. This could have caused your seg fault.
parsant
2016-12-12 09:48:29 UTC
This wasn't what you asked, inspite of the undeniable fact that this is significant: in case you will use printf() to output a string, you will desire to easily approximately constantly use %s--despite if this is a unmarried string. If the string comes from person enter, take off the "in basic terms approximately" and make that usually, constantly, constantly use %s. in case you utilize printf(myString1) and myString1 has formatting characters in it, this is a clarification for something from incorrect output to a gaping hollow in gadget protection. Use places() or fputs() to output strings with out any formatting. (nicely, in basic terms approximately none...places() does upload a newline, mutually as fputs() would not.) As on your question, no...there is no longer something like string concatenation in C. C++ has a string variety that overloads + as a concatenation operator, yet no longer C. i take advantage of distinctive %s formats each each now and then to do in basic terms what it seems such as you desire to do: shield demonstrate screen area mutually as conserving a function readable.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...