Strings in C are represented as arrays of characters.
Copychar *p = "String";
You are declaring a pointer that points to a string stored some where in your program (modifying this string is undefined behavior) according to the C programming language 2 ed.
Copychar p2[] = "String";
You are declaring an array of char initialized with the string "String" leaving to the compiler the job to count the size of the array.
Copychar p3[5] = "String";
You are declaring an array of size 5 and initializing it with "String". This is an error be cause "String" don't fit in 5 elements.
char p3[7] = "String"; is the correct declaration ('\0' is the terminating character in c strings).
Reference
Answer from ob_dev on Stack OverflowCan someone please tell me how to declare a string in C and read them with scanf()? Tutorials point/geekforgeeks/other internet resources are total garbage and hard to follow for beginners.
How do strings work in C
[Caesar]How to declare an empty string array in C?
(C) If I declare a string array without using malloc(), do I need to still free() it? Example below.
Can you declare a string without initializing it in C?
How to initialize a string in C?
What is the difference between declaration and initialization of strings in C?
Videos
Strings in C are represented as arrays of characters.
Copychar *p = "String";
You are declaring a pointer that points to a string stored some where in your program (modifying this string is undefined behavior) according to the C programming language 2 ed.
Copychar p2[] = "String";
You are declaring an array of char initialized with the string "String" leaving to the compiler the job to count the size of the array.
Copychar p3[5] = "String";
You are declaring an array of size 5 and initializing it with "String". This is an error be cause "String" don't fit in 5 elements.
char p3[7] = "String"; is the correct declaration ('\0' is the terminating character in c strings).
Reference
This link should satisfy your curiosity.
Basically (forgetting your third example which is bad), the different between 1 and 2 is that 1 allocates space for a pointer to the array.
But in the code, you can manipulate them as pointers all the same -- only thing, you cannot reallocate the second.
Hi,
So tutorialspoint, geeksforgeek, javatpoint, those websites that claim to have C/C++ tutorials are a bunch of shitty Indian garbage tutorials that are not suited for beginners at all. I'm trying to learn how to declare a string variable with C and read input with scanf() and I get a bunch of different information from all three sites and I'm really frustrated and confused. Do you do?
#include <stdio.h>
int main(void)
{
char name[100000000];
printf("What's your name?\n");
scanf("%c", char);
}Or is it
#inclue <stdio.h>
int main(void)
{
char** x;
printf("What's your name?\n");
scanf("%c", x)
}Or is it
#include <stdio.h>
int main(void)
{
string x;
printf("What's your name?\n");
scacnf("%s", x);
}Or is it
#include <stdio.h>
int main(void)
{
char **name;
printf("What's your name?\n");
scanf("%c", name)What the fuck is it? Is it %c or %s for the format code, and it is char name[] or char **var_name or **char var_name?? Someone please tell me. All these online tutorials are fucking GARBAGE and I can't find a clear answer anywehere
There are multiple ways to create a string in C:
char* string1 = "hi";
char string2[] = "world";
printf("%s %s", string1, string2)I have a lot of problems with this:
According to my understanding of [[Pointers]], string1 is a pointer and we're passing it to [[printf]] which expects actual values not references.
if we accept the fact that printf expects a pointer, than how does it handle string2 (not a pointer) just fine
I understand that char* is designed to point to the first character of a string which means it effectively points to the entire string, but what if I actually wanted to point to a single character
this doesn't work, because we are assigning a value to a pointer:
int* a; a = 8
so why does this work:
char* str; str = "hi"