Is there problem to declare an empty char array?

Pages: 12345
Use a string. A std::string. They can resize as needed.
closed account (42TXGNh0)
Thanks but I want a method that can be also used in C. :P
How can I ever decide the size of the array before knowing the size of the string...?


You want a way of knowing how much memory you need, before that information actually exists? You want to know some information, before that information exists?

You can't. So come up with a method that doesn't rely on you knowing information that doesn't exist.
closed account (42TXGNh0)
I've just think of a method, will this also lead me to the realm of undefined behavior?
1
2
3
static char * c;
c = "This is a sentence.";
printf("%s",c);
closed account (42TXGNh0)
(It gives me this warning which I don't understand what it means: [Warning] control reaches end of non-void function [-Wreturn-type])
That's fine. The text "This is a sentence." is placed in memory, and the pointer points at it. There is no string copying here.

Although static char * c should really be static const char * c.
Last edited on
closed account (42TXGNh0)
Why and what's the difference?
Because "This is a sentence." is a pointer to a const char.

Why is it static? Do you know what static means?
Last edited on
closed account (42TXGNh0)
It keeps the variable exists even after the function has returned. (That's all I know. ._.)
control reaches end of non-void function


This means your function is meant to return something, but it isn't returning anything.

Thus far, I've not seen any problems you're having. What is it that you actually want to do, in C, that you can't do, because you don't know how big a string is?
closed account (42TXGNh0)
Nope, is that I can't use std::string in C. So I gotta find another way to do it, just as my method above.
1
2
const char* c = "This is a sentence.";
printf("%s", c);


No need for messing around with static. If the actual characters are always known, and you can type them into the source code, you don't need to bother with copying or assigning memory.
Are we overthinking this?
the standard C design pattern for the past 50 years is to have a max size for your strings. Say you don't expect the strings you generate in your program to exceed 100 characters.

then it just looks like this.

#define maxsize 100;

int main()
{
using namespace std;

char c[maxsize] = "";
strcpy(c,"This is a sentence.");
printf(c);
}


problem solved. It wastes a tiny bit of your stack memory.


if you need to go there, you can alloc and realloc a pointer to the exact size you need for each string you handle, but this is slow and its overkill. Don't do this until you NEED to do it. The answer isn't that you know how big stuff will be before you make it, but that you probably have a rough idea of what the biggest thing you will make is going to be, and give a fudge factor over that (say 25% bigger than the biggest thing you know you need) and you just make them hold that much. See if it works. If it does not work, review the input that broke it during testing and adjust the size.

if your code is in a place where a buffer overflow attack can be an issue, you will need to review approaches to handling that.
Last edited on
const char* c = "This is a sentence.";

Here, "This is a sentence" is an rvalue right?
Assigning a pointer requires address so how can you make a pointer point to an rvalue which has no address in memory?

I'm a newbie with pointers.

Also if "This is a sentence" were stored in a string or array of characters, what are you doing when you write const char* c = variable;?? You must use & to point to the first index of the variable right?

Confusing.
> Here, "This is a sentence" is an rvalue right?

No, it is an lvalue of type 'array of 19 const char'; it is an object with static storage duration.
A string literal is an lvalue; all other literals are prvalues. http://eel.is/c++draft/expr.prim.literal




> what are you doing when you write const char* c = variable;??
>You must use & to point to the first index of the variable right?

Array-to-pointer decay
There is an implicit conversion from lvalues and rvalues of array type to rvalues of pointer type: it constructs a pointer to the first element of an array. This conversion is used whenever arrays appear in context where arrays are not expected, but pointers are.
https://en.cppreference.com/w/cpp/language/array#Array-to-pointer%20decay
Last edited on
So the compiler is storing "This is a sentence" in memory is it? But only the pointer knows where the memory is? Will the memory storing "This is a sentence" also go out of scope when the pointer pointing to this goes out of scope, and will it go out of scope even if there is another pointer pointing at the location which is still in scope?

Last edited on
The string literal object is an object with static storage duration. The storage for the object is allocated when the program begins and the object exists till the program ends.
Confusing.

string var = "hello";
Over here, is "hello" a string literal object who's been allocated for at the beginning of the program, and var is a pointer to it?

Now consider:
1
2
string var = "hello";
string var = "Hello there";


Are both "hello" and "hello there" string literal object who have been allocated for at the beginning of the program?


But if we consider integers,
int var = 5;

Here 5 is being inserted to location pointed to by var right?

how about
 
string var = 'h';


which is not a string but a character. What is happening here? To the location of var, we are putting 'h' like in the case of integers?


Then why do we need to have literal objects at all? Why can't we assign characters to indexes of the location like we did with integers because then we don't have to waste memory for something that might be temporary right?
So,

 
char var[3] = "hi";


vs

1
2
3
4
char var[3];
var[0] = 'h';
var[1] = 'i';
var[2] = '\0';


The two snippets are different? "hi" is a string literal that will be allocated for at the beginning of the program whereas in the second example there is no string literal.

This brings up a question. Why do we use char var[3] = "hi"; at all then? Because in the first example if we assign a new string literal, then the old string literal would be of waste of memory and unaccessible by anything (other than a pointer pointing to its address). Whereas in the second example there's no string literal, you are just assigning new characters to the different indexes.

confusingggggggg :'(
Last edited on
> Why do we use char var[3] = "hi"; at all then?

It is convenient. It is simpler than writing: char var[3] = { 'h', 'i', '\0' };
Pages: 12345