r/C_Programming • u/Sea_Competition_8645 • Jun 26 '26
Question Can someone please explain in simple terms
Im struggling to understand when to use char* and when not to use (especially in functions). Doesn't an arrays first element acg as pointer? So why do we need to use char *var[50]; for example? Whats the difference? And why dont we use that for char
2
Upvotes
14
u/lfdfq Jun 26 '26
char* is just a pointer to a character. It could point to a single character, or it could point to the start of an array (or other allocation), or it could be NULL.
One common use for char* is to represent a string (a sequence of characters), and is usually terminated by a NUL (i.e. a 0 byte).
char *var[50] is an array of char*, i.e. it's 50 pointers, not an array of 50 chars. char var[50] would be an array of 50 chars.
If you had char* p = var, then p would essentially be a pointer to the start of the array, yes.
10
u/Limp-Confidence5612 Jun 26 '26
a[b] = *(a + b)
this is the part that you should internalize.
array notation is just syntactic sugar for dereferencing a base address at a specific offset.
12
u/PurpleBudget5082 Jun 26 '26
That's actually one of the complicated concepts to understand in C, char* var[50] is an areay of 50 char*, because [] "binds tighter" than *.
char* is a pointer to a char, if you want to use it as a string you should allocate the size of the string + 1, and put '\0' into the last element. C runtime will interpret everything up to \0 as a string.
4
u/enygmata Jun 26 '26
I'm rusty but I believe char* var[50] means a variable that holds 50 pointers to char. That may mean the variable can point to 50 different strings or point to 50 different characters, depending on the context. In any case, the variable does not hold any characters or strings.
2
u/rollowicz Jun 26 '26
Declaring an array like char var[50] allocates memory for 50 char elements, but yes the type of var is essentially the same as a pointer to the first element. So you can pass the array var to a function taking a char * pointer, for example a function void f(char * p) can be called as f(var). You have to keep track of the number of elements in the array (50) yourself, usually by passing this number as a second argument to functions. The declaration char * var[50] on the other hand creates an array of 50 char *, that is, 50 pointers to char -- which is probably not what you want.
2
u/SmokeMuch7356 Jun 26 '26 edited Jun 26 '26
Given the declaration
you get this in memory (addresses are just for illustration):
You can modify the contents of
strto your heart's content, as long as it fits in 6 elements:The object designated by
stris not a pointer; it is an array of 6 character elements. Under most circumstances, the expressionstrwill evaluate to a pointer to the first element of the array.1 So when you call a function likethe expression
strevaluates to a pointer with the address value0x8000, and that's whatfooreceives:Since all the function receives is a pointer, it doesn't know how big the array is.
If the function is expecting a string, it can look for the
0terminator to determine how long the string is, but that's not the same as the size of the array that stores the string. If the function has to know the size of the array (so as not to write past the end of it), we have to pass that as a separate parameter:Given the declaration
you get this in memory:
In this case, the object designated by
stris a pointer, and stores the address of the first character of the string literal"hello". The storage for the string literal itself is taken from somewhere else (usually a read-only data segment). The behavior on attempting to modify the contents of a string literal is undefined - it may work as expected, it may fail silently, it may crash, it may start mining Bitcoin. After all, you can't modify numeric literals likeso the same should be true for string literals. However, unlike numeric literals, string literals require storage, and historically some platforms stored them in a way such that they could be modified.
So, to be safe, when declaring a pointer to a string literal, declare it
const:so if you accidentally try to modify it through
str, the compiler will yell at you.This creates an array of pointers to
char; we'd typically use it for something like:which would give us
Again, since we're creating pointers to string literals, we're declaring it
constso if we accidentally try to modify one of the strings throughvar[i]the compiler will yell at us.We could also use this to store pointers to local arrays:
or dynamically allocated arrays:
or some unholy combination of pointers to static, local, and dynamic arrays.
C was derived from an earlier language named B, and when you declared an array in B:
a separate pointer object was created to store the address of the first element; the array subscript operation
a[i]was defined as*(a + i)- given the address stored ina, offsetiwords and dereference the result. When he was designing C, Ritchie wanted to keep this subscripting behavior, but he didn't want to keep the separate pointer it required. So he created the rule that array expressions evaluate to pointers:a[i]is still defined as*(a + i), but ifais an array expression, it is implicitly converted to a pointer expression.1
u/jontsii Jun 26 '26
In C, strings don´t exist they are char *, you can think of char * as a string. Then char pointer arrays are arrays of strings
1
u/HashDefTrueFalse Jun 26 '26
Recent and relevant comment I wrote here, in case it helps you understand the difference between arrays and pointers to array elements in C.
1
u/Drazev Jun 26 '26
The high level explanation is that while they have the same meaning, there are different norms for when to use which notation and the compiler behaviour for using them may produce extra warnings for some compilers, but never depend on that if they do. The [] notation is often used when the programmer views the variable as some constant value they. can lookup. The pointer notation is common when the variable represents something that is described by a starting value and offset pair, or a stream of information. For example when dealing with physical memory registers or network communication I find most use pointers and offsets.
Keep in mind they are functionally the same because the C compiler basically translates all your code to the machine level where many of your programming concepts, like variables, do not exist. Pointers are the natural state of things for the compiler since the machine only deals with addresses and words. Typing and array notation provide the compilers extra information that it can use to validate your code to potentially find errors during the compilation stage. This also means that some compilers could potentially produce extra warnings depending on how they want to treat the [] notation. Don’t count on that though because they are the same by a C standard so any compilers that does that would be special. For clarity a compiler is normally specific to a machine and OS/platform, so the company providing it could sometimes modify the base compiler code to add or remove extra features like flags that produce warnings.
1
u/jwzumwalt Jun 27 '26
In many of my programs, especially those that may be viewed by inexperianced programmers, I use the following typedef.
// typedefs
typedef char string; // alias "string", more descriptive than "char"
Then in the program I use "string" instead of "char".
Once you understand char, you will understand why I use the "string" typedef :-)
1
u/Radiant-Cow5217 Jul 01 '26
It is used to hold an array of char arrays. Each array is pointed by one element of the outer array. like i hold 50 lines of characters (50 char pointers) in var.
0
u/Ander292 Jun 26 '26 edited Jun 26 '26
char c => 1 byte, single character
chat c[68] => An array for 67 characters (68 bytes)
char *c => Pointer to a character or a string (4 bytes)
char *c[12]=>An array of pointers to a char or a string (12 times 4 bytes)
Also no, the first element of an array is not the pointer to it. While you are inside the function you created the array (or its a global variable) then, in simple terms, the compiler will "remember" whats the address of the array. When you pass it to a function it decays to a pointer which you can dereference and use the same way like u used an array. This is why you can do sizeof() on an array but only inside the scope where its created.
Edit: Pointer size depends on architecture. Ex. Its 4 bytes of 32bit systems and 8 bytes on 64bit systems.