Jump to content

How to simulate String type from Java in C?

Arphenyte

Like, I've been wondering this since forever, because my first language was Java and it was so easy to manage Strings, I just needed to create a String variable and I could insert X amount of chars in it, while on C I need to create arrays and give it a size, and I don't like that, more than not liking it, it's like I know there must be a way to make a String in C that does not require you to give a char array a size. Something like this:

char a[];

In which I don't need to specify the size of the array in order for the user to store a String of any given amount of chars. Like I know there must be a way to simulate the String type in C, but I just don't know how... I can simulate an Object Array in C, but not this... Unbelievable...

Link to comment
Share on other sites

Link to post
Share on other sites

char awesome[] = "String";

Or am I missing something?

 

 

You can also do it via pointer:

char *awesome = "String";
Link to comment
Share on other sites

Link to post
Share on other sites

You can write a string struct like this:
 

struct MyString{  char* data;  unsigned int length;};

And just add all the functionality you need by writing functions. Down below are some examples:

MyString CreateFromCString(const char* c){  MyString ret;  ret.length = strlen(c);  ret.data = (char*)malloc((ret.length + 1) * sizeof(char)); memcpy(ret.data, c, ret.length * sizeof(char));ret.data[ret.length] = '\0'; return ret;}MyString StringAdd(MyString *left, MyString *right){  MyString ret;  ret.length = left->length + right->length;  ret.data = (char*)malloc((ret.length + 1) * sizeof(char));  memcpy(ret.data, left->data, left->length * sizeof(char));  mempcy(ret.data + left->length, right->data, right->length * sizeof(char));  ret.data[ret.length] = '\0';  return ret;}

There isn't really any other way in C since you don't have constructors and destructors, and there is no operator overloading. If you want nice strings you might want to look into using C++, since you do get those features in that language and there is a string class available in the STL.

Link to comment
Share on other sites

Link to post
Share on other sites

char awesome[] = "String";

Or am I missing something?

 

 

You can also do it via pointer:

char *awesome = "String";

That is making the preproccessor assign the string value ("String") to the array while compiling. That is not what I'm looking for. I'm looking for something like this:

char awesome[];printf("Insert a string\n");fflush(stdin);scanf("%s",awesome);printf("%s",awesome);

This will probably result in an error in C.

Link to comment
Share on other sites

Link to post
Share on other sites

char awesome[] = "String";

Or am I missing something?

 

 

You can also do it via pointer:

char *awesome = "String";

There is a difference between the two. The first one is an array that contains that string. The other is a pointer to a string literal. Changing the first string is legal. Changing the second string will result into an error.

Link to comment
Share on other sites

Link to post
Share on other sites

That is making the preproccessor assign the string value ("String") to the array while compiling. That is not what I'm looking for. I'm looking for something like this:

char awesome[];printf("Insert a string\n");fflush(stdin);scanf("%s",awesome);printf("%s",awesome);

This will probably result in an error in C.

 

Completely misunderstood the question sorry. Else I know of no elegant way to do that, best way would be custom struct and functions.

Link to comment
Share on other sites

Link to post
Share on other sites

That is making the preproccessor assign the string value ("String") to the array while compiling. That is not what I'm looking for. I'm looking for something like this:

char awesome[];printf("Insert a string\n");fflush(stdin);scanf("%s",awesome);printf("%s",awesome);

This will probably result in an error in C.

In C you'll have to do all memory allocations yourself. What you usually do is creating a temporary string on the stack with a fixed size, and then allocating a string of the correct size and copying it over.

 

//ask user for stringchar buffer[128];printf("Insert a cool string");fflush stdin);scanf("%s", buffer);//allocate string and copy it from our temp buffer.unsigned int size = strlen(buffer) + 1;char* str = (char*)malloc(size);memcpy(str, buffer, size);//do cool stuff with your string, maybe create some more strings using the same buffer//don't forget to clean upfree(str);
Link to comment
Share on other sites

Link to post
Share on other sites

 

In C you'll have to do all memory allocations yourself. What you usually do is creating a temporary string on the stack with a fixed size, and then allocating a string of the correct size and copying it over.

 

//ask user for stringchar buffer[128];printf("Insert a cool string");fflush stdin);scanf("%s", buffer);//allocate string and copy it from our temp buffer.unsigned int size = strlen(buffer) + 1;char* str = (char*)malloc(size);memcpy(str, buffer, size);//do cool stuff with your string, maybe create some more strings using the same buffer//don't forget to clean upfree(str);

Yes! That is what I mean! What if I insert a string of 129 chars?

char buffer[128];

Then this will render completely useless, your above struct and functions makes for a more valuable solution for unlimited chars on a String than this.

Link to comment
Share on other sites

Link to post
Share on other sites

Yes! That is what I mean! What if I insert a string of 129 chars?

char buffer[128];

Then this will render completely useless, your above struct and functions makes for a more valuable solution for unlimited chars on a String than this.

There might be a platform specific thing for that, but I don't know of any way to do that with the standard library.

maybe this can help you if you happen to be programming on windows: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx

Link to comment
Share on other sites

Link to post
Share on other sites

Make a method that calls realloc() on the array (assign the pointer returned to a temporary variable and check if it's null first), with the length of your string + the length of the string you want to add, then use strcat on them to bond them.

Alternatively you could allocate more memory to the initial string, to avoid a realloc call, which can be expensive.

 

It might be a good idea to make a struct with a char array and a length property, so that you can keep track of where it ends more easily. string delimiter characters can get annoying.

 

also, you can do a max number of characters on scanf.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×