Vissa kompilatorer kräver det, till exempel MCC18.Det finns väll en ganska stor skillnad, sodjan har med const i sin version.
Eftersom det är en konstant och inget annat.
Vissa kompilatorer kräver det, till exempel MCC18.Det finns väll en ganska stor skillnad, sodjan har med const i sin version.
Kod: Markera allt
#include <stdio.h>
sVarabel = sprintf("TEXT SOM SKRIVS");
lcd_puts(&sVarabel);
sVarabel = sprintf("Varvtal: %d RPM", rpm);
lcd_puts(&sVarabel);
Kod: Markera allt
const rom char string[] ="this is a string"; //är ok i MCC18
Kod: Markera allt
const rom char * const string= "this is a string";
Kod: Markera allt
int main(int argc, char* argv[])
{
char test1[] = "korvbil A";
char * test2 = "korvbil B";
char * test3 = test1;
//char test4[] = test2; // Ej tillåtet
printf("test1: %s, sizeof(test1) = %d\n", test1, (int)sizeof(test1));
printf("test2: %s, sizeof(test2) = %d\n", test2, (int)sizeof(test2));
printf("test3: %s, sizeof(test3) = %d\n", test3, (int)sizeof(test3));
return 0;
}
Kod: Markera allt
test1: korvbil A, sizeof(test1) = 10
test2: korvbil B, sizeof(test2) = 4
test3: korvbil A, sizeof(test3) = 4
Kod: Markera allt
Q: What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
My program crashes if I try to assign a new value to p[i].
A: A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways:
As the initializer for an array of char, as in the declaration of char a[] , it specifies the initial values of the characters in that array (and, if necessary, its size).
Anywhere else, it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. In an expression context, the array is converted at once to a pointer, as usual (see section 6), so the second declaration initializes p to point to the unnamed array's first element.
Some compilers have a switch controlling whether string literals are writable or not (for compiling old code), and some may have options to cause string literals to be formally treated as arrays of const char (for better error catching).
See also questions 1.31, 6.1, 6.2, 6.8, and 11.8b.