string in c programming

eetech00

Joined Jun 8, 2013
4,711
I don't know that "most would use a pointer". Pointer syntax is, for the most part, syntactic sugar, but it's there for a reason. It makes the code much more readable for most people.

It also relieves the writer and reader from having to remember fine details of the precedence of operators. For instance, whether *cptr++ evaluates to (*cptr)++ or *(cptr++). Even if the writer gets it right, it can cause confusion on the part of the reader, either because they get it wrong or they have to spend time recalling it or looking it up. The array notation is completely unambiguous.

If you don't need the char array to be exactly 15 characters long, a better way to declare it is just

unsigned char a[] = "ELECTRONICS";

and let the compiler determine how much memory to allocate. That way if you lengthen the string later you don't create a bounds violation.
If fact, the way the OP assigned the string probably would cause a buffer overflow..
 

WBahn

Joined Mar 31, 2012
32,999
Hi

my take on this:

If a char variable is Initialized with a specific length, then there is no null character unless it is assigned to an element in the array.

char [2] = "ab"; /no null terminator

If the char variable is initialized with an unspecified length, then a null termination character is placed in the array.

char [] = "ab"; / null terminated
Not correct.

First, you need an array name. But even after you provide that, the top code will compile and run on most compilers, but it invokes undefined behavior because you are writing three bytes to an array that is only dimensioned for two. But the NUL terminator WILL be copied to the array.
 

WBahn

Joined Mar 31, 2012
32,999
If fact, the way the OP assigned the string probably would cause a buffer overflow..
Why? The array is dimensioned for 15 elements. The string that is used to initialize the array contains twelve (eleven character codes and a NUL character).

A string literal IS a NUL terminated sequence of char values.
 

eetech00

Joined Jun 8, 2013
4,711
Not correct.

First, you need an array name. But even after you provide that, the top code will compile and run on most compilers, but it invokes undefined behavior because you are writing three bytes to an array that is only dimensioned for two. But the NUL terminator WILL be copied to the array.
Yes...a typo. Should be:

char s[2] = "a";
 

eetech00

Joined Jun 8, 2013
4,711
Why? The array is dimensioned for 15 elements. The string that is used to initialize the array contains twelve (eleven character codes and a NUL character).

A string literal IS a NUL terminated sequence of char values.
Because the statement:

unsigned char a[15] = "ELECTRONICS";

initializes variable "s" to be an array of 15 integers numbered 0-14. The lower bound is 0 and the upper bound is 14, but the statement writes "ELECTRONICS" to element 15 (out of bounds). Buffer overflow!

The statement should be:

unsigned char a[] = "ELECTRONICS";

So, variable "a" would contain:
{'E','L','E','C','T','R','O','N','I','C','S','\0'}

eT
 

MrSoftware

Joined Oct 29, 2013
2,273
Of course you won't use numbers in your code for string lengths, you would use variables or #define, etc.. it was just easier to demonstrate the point with minimal changes to the original code. That said:

The correct method is to let the compiler do it as @WBahn mentioned in the post just before yours. If the string is being assigned dynamically, you can add code to malloc enough space. That way, the string will always be constructed properly and kludgey code can be avoided.
In regards to this method:

Code:
unsigned char a[]="ELECTRONICS";    //string of STRINGMAX characters plus a NUL terminator.
    lcd_init();
   for ((i = 0; i < ( sizeof(a) && a[i] ); i++) // Check array bounds then look for NUL
Is it part of the C standard that the compiler will give you the size of the array in this case? Going by memory, I'm thinking I ran into a case where sizeof(a) gave me sizeof(char) in an older C compiler.
 

WBahn

Joined Mar 31, 2012
32,999
Because the statement:

unsigned char a[15] = "ELECTRONICS";

initializes variable "s" to be an array of 15 integers numbered 0-14. The lower bound is 0 and the upper bound is 14, but the statement writes "ELECTRONICS" to element 15 (out of bounds). Buffer overflow!
You need to refresh your memory on how array initialization works.

First off, the code

Code:
unsigned char a[15];

a[15] = "FRED";
is illegal because the left side of the assignment operator is a char variable (the 15th element of the array pointed to by a) while the right side of the assignment is a pointer to a char.

Combined, however, it is an array initializer. The following two statements are identical:

Code:
unsigned char a[15] = {'F','R','E','D','\0'};
unsigned char a[15] = "FRED";
The second is syntactic sugar for the first.

If you don't believe me, try it.
 

WBahn

Joined Mar 31, 2012
32,999
Is it part of the C standard that the compiler will give you the size of the array in this case? Going by memory, I'm thinking I ran into a case where sizeof(a) gave me sizeof(char) in an older C compiler.
Yes, it is part of the standard: "When applied to an operand that has array type, the result is the total number of bytes in the array."

This is from the C99 Draft Standard Section 6.5.3.4 The sizeof operator.

I believe it was part of the prior standard as well.

One very useful application of the sizeof operator is to get the number of elements in an array.

For instance:

Code:
int fred[10][15];

unsigned int arraySizeBytes = sizeof(fred);
unsigned int arrayElements = sizeof(fred) / sizeof(**fred);
unsigned int arrayRows = sizeof(fred) / sizeof(*fred);
unsigned int arrayCols = sizeof(*fred) / sizeof(**fred);
unsigned int arrayElementBytes = sizeof(**fred);
 
Top