I create the array of a product description, I leave it blank because I will use it later:
char Desc[70];
After asking a user for keys I want to assign (now yes) a string to my array depending on the key:
if (strcmp(clave,"2SGL")==0)
{
tarifa = 30.00;
Desc[70]="Producto X";
}
The problem is that when I want to print the arrangement with the product description, none of it appears on the screen. How can I solve that?
Delving a little deeper into @AlvaroMontoro's answer and giving technical details of how literal strings work .
When quoted text "Like This" appears in your C program (technically called a string literal ), to the compiler the entire text is nothing more than a number, usually 32 bits.
So when you did:
Basically it is as if you had done:
How is that?
To be more precise, the compiler saves all the text between quotes in a part of the executable, and "takes note" of where that text is in the executable. All the texts that your program has "Between quotation marks" are compiled one after another in that area of the executable.
When the program is executed, it is loaded from disk to memory, so all the quoted messages used by your program will end up in nearby memory areas. In the code that made use of these messages, instead of the message, what is used is the memory address where that message in question has ended up.
An example. Suppose in a part of your code you have something like:
The compiler, seeing the string
"uno"
, encodes it into Ascii and adds a terminator to it (thus taking up 4 bytes) and leaves it at a certain position in the executable it is building. It then does the same with the string"dos"
, which will probably end up after the previous string inside the executable. With binary file inspection tools, you could open an executable and discover all its strings together, in the same area.When the program is loaded into memory, let's say the area of the executable that contained the strings is loaded to memory address 0x50004000. The first string (
"uno"
) will start at that address and will be 4 bytes long. The following string ("dos"
) will therefore start at address 0x50004004.Well, the line in which you called
strcmp("uno", "dos")
is converted to machine code that would be equivalent to:Each string in quotes is replaced by the compiler with a number, which is the memory address where the string will be stored in memory when the program is loaded. That is, each quoted string is actually a pointer to where the text is.
This is why any function that expects a
char*
can be passed a quoted literal string.That's why it also works as the second parameter of
strncpy(Desc, "Producto X", 70);
, and in that case the function will make a copy of the string from the place where it is pre-stored to the address of the variableDesc
(70 is the maximum number of characters to copy, butstrncpy()
it will stop before , when you find the terminator.Going back to your code:
You are putting a number in position 70 of the array. Since that number is typically 32 bits, it won't fit in one array position (since a
char
is 8 bits). Naturally the compiler will have given you a warning on that line, but if you ignore it, it will probably save only the lower part of the data (08
) and discard the rest. In any case, the problem is not only that you are accessing an invalid element of the array, but also that what you are saving there is not at all what you thought. You are not saving the text, but the address where the text is (and wrong, because it does not fit).The code you share has a couple of problems, but they basically boil down to the same thing: this is not how you assign an array of characters.
As you are doing right now, on the one hand you define an array of characters of size 70 (
char Desc[70];
), which is correct, but then when doing the assignment everything fails because you are using the wrong notation:The brackets with the size of the array only do it in the declaration, not in the assignments. Otherwise you would only be assigning the value to that particular position instead of the entire variable. Which is going to be a problem in itself, because you would be assigning to a memory location that would not correspond to the variable (70 is outside the allowed indices: 0..69) which can generate unexpected results later on.
To assign a string like that, you could use the function
strncpy
that will copy a string to the specified variable. The parameters ofstrncpy
are the variable you want to assign the value to, the string you want to assign, and the number of characters to copy. Applied to your code it is:Here you can see a demo running on OnlineGDB .