ProgrammingC Developer

How is string manipulation implemented in C? Explain the difference between a character array and a string pointer. How can errors in string handling be avoided?

Pass interviews with Hintsage AI assistant

Answer

In C, strings are implemented as arrays of characters, ending with a null byte ('\0'). Example of string declaration:

char str1[] = "hello"; // Array with 6 characters: {'h','e','l','l','o','\0'} char *str2 = "hello"; // Pointer to a string literal
  • Character array: Memory is allocated for each element, including the terminating null. The array can be modified (unless it is const).
  • String pointer: Can point to a string literal (which cannot be modified) or to an allocated memory block. String literals are typically stored in the read-only segment.

To avoid errors:

  • Properly manage memory (use malloc, strcpy, check buffer size).
  • Do not use string literals for modifying strings.
  • Ensure all strings are null-terminated \0.

Example of correct string handling:

char buffer[100]; strcpy(buffer, "test"); // OK, buffer is mutable and guaranteed to contain '\0'

Tricky Question

What is the result of executing the following code and what errors will it lead to?

char *str = "hello"; str[0] = 'H'; printf("%s\n", str);

Answer: The program will lead to undefined behavior, most likely a segmentation fault, because string literals are placed in read-only memory. Writing values at the address of a string literal is not allowed.

Examples of Real Errors Due to Lack of Knowledge


Story The team confused the concepts of string array and pointer to literal. One function took char *output = "default"; and later executed strcpy(output, input);, which resulted in a crash on the first run, because the copy was occurring in read-only memory.


Story When working with the network, the result was written to a buffer allocated with char *buf = NULL; strcpy(buf, data);. This led to writing to uninitialized memory and crashing the application.


Story In a localization package, the team passed strings between components without ensuring that a \0 character was added. Once, the function printed garbage to the console and corrupted internal memory structure.