I am supposed to answer a homework question for one of my classes. Specifically, I am supposed to say if certain arrays in C are considered strings or not. Based on this article (https://www.geeksforgeeks.org/strings-in-c-2/) I know that strings are an array of characters with the null terminator at the end.
My main hangup is a part of the question that asks about an array that looks like this:
char c1[] = { 'C', 'S', '\0', '3', '2', '4', '\0' };
This is obviously an array of characters with a null terminating character at the end. However, is it still considered a string since it also has a null terminating character in the middle? How will that affect the string?
EDIT: Based on comments, I have provided the actual wording of the question:
"Which of the following arrays can be considered "strings" for the purposes of using them as arguments to strcpy(), strncpy(), strcmp(), strncmp(), and similar string functions (indicate all the apply)?"
EDIT: I emailed my professor about it since the question seemed ambiguously worded (as several people pointed out). If anyone is curious, he told me "Yes it is a string. The key is that there is a null character. But of course that will effect any string operations; the string ends at the null character."
Introduction to String in C. String in C is defined as an array of characters that are terminated with a special character (Null character) ‘\0’. So a non-finished string includes the characters consisting of the list preceded by a null. Defining a string is similar to defining a one-dimensional array of characters.
String in C is defined as an array of characters that are terminated with a special character (Null character) ‘\0’. So a non-finished string includes the characters consisting of the list preceded by a null.
Strings in C. Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘0’.
A single character is defined using single quote representation. A string is represented using double quote marks. Example, "Welcome to the world of programming!" ‘C’ provides standard library <string.h> that contains many functions which can be used to perform complicated operations easily on Strings in C.
c1
is mostly [1] equivalent to &c1[0]
, which is holding one string, "CS"
.
There's a second string lurking in there, "324"
, starting at &c1[3]
-- but as long as you access c1
as c1
, the string "CS"
is all the functions strcpy()
et al. would see.
[1]: c1
is an array, &c1[0]
is a pointer.
If you want to know the specifics of the definition of a string in C, go to the source.
From the C90 standard:
7 Library
7.1 Introduction
7.1.1 Definitions of terms
A string is a contiguous sequence of characters terminated by and including the first null character. A “pointer to” a string is a pointer to its initial (lowest addressed) character. The “length” of a string is the number of characters preceding the null character and its “value” is the sequence of the values of the contained characters, in order.
(There were no relevant changes in later standards.)
Thus, c1
contains two consecutive strings, "CS" and "324", but is not itself a string.
If we pass an array to a function, it decays to a pointer to its first element, thus +c1
points to a string (the first one), which is good enough for any function expecting a pointer to string. It doesn't point to a string "CS\0324", but that's probably good enough for your instructors question, which is ambiguous.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With