Is it Correct to Replace '\0' with a Website Address in a Char Array?

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Array Pointers
Click For Summary
SUMMARY

The discussion centers on the incorrect replacement of the null terminator '\0' in a char array with a website address. The code example provided, char *strings[4] = { "Monday morning", "6AM", "Sunrise", "www.iota-six.co.uk" };, illustrates that this declaration creates an array of four pointers to char, not an array of four character cells. Each string in the array is automatically null-terminated by the compiler, and replacing the null terminator with a string like "www.iota-six.co.uk" is fundamentally incorrect, as every char array must end with '\0' to function properly.

PREREQUISITES
  • Understanding of C programming language syntax
  • Knowledge of pointers and memory management in C
  • Familiarity with string handling and null termination in C
  • Basic concepts of array declarations in C
NEXT STEPS
  • Study the differences between char *strings[4] and char strings[][4] in C
  • Learn about pointer arithmetic and memory allocation in C
  • Explore the implications of null termination in string handling
  • Investigate best practices for declaring and initializing arrays of strings in C
USEFUL FOR

C programmers, software developers working with C, and students learning about pointers and string manipulation in C will benefit from this discussion.

transgalactic
Messages
1,386
Reaction score
0
in this code i got a chat type pointer which is an array
who consists out of 4 char cells

in order that each pointer cell of this array will point to some place
we need to give it an address some thing like &chr

"Monday morning" is not an address and its not a single char

this misunderstanding is for every value in this array

and we have 4 cells and in char arrays the last cell has to be \0
so there only place for 3 values

the 4th cell is www.iota-six.co.uk instead of \0

but its wrong

every char array must end with \0
and here we changed \0 to www.iota-six.co.uk

this is wrong

??
Code:
char *strings[4] = { "Monday morning" , "6AM" ,"Sunrise" , "www.iota-six.co.uk"};

??
 
Technology news on Phys.org
transgalactic said:
Code:
char *strings[4] = { "Monday morning" , "6AM" ,"Sunrise" , "www.iota-six.co.uk"};
in this code i got a char type pointer which is an array
who consists out of 4 char cells
What you declared is a valid (but better as a const char * array), useful construct that is used quite often.

You explanation is not what is happening. To begin, char *strings[4] declares an array of 4 "pointers to char", not an indeterminately sized array of character cells of 4 characters each. Big difference. The latter is char strings[][4];.

"Monday morning" is not an address and its not a single char
You don't want a single character with the above declaration. What you want is a set of things that are can be interpreted as a pointer to a character -- and that is exactly what "Monday morning" is.

It looks like you might be taking too big a bite of the C language. You first should understand why this works:
Code:
   const char * string = "Monday morning";
   printf ("As of my post time, it is ridiculously early on %s\n", string);
   string = "Monday afternoon";
   printf ("But if I am lucky I might be able to sneak in a nap on %s", string);
 
Code:
char *strings[4] = { "Monday morning" , "6AM" ,"Sunrise" , "www.iota-six.co.uk"};
What you get is Four pointers. Your declaration is like below:

char * strings[0]= "Monday morning";
char * strings[1]= "6AM";
char * strings[2]= "Sunrise";
char * strings[3]= "www.iota-six.co.uk"

NOTE: I write these code above to demonstrate a "pointer array" definition. You can't compile it in the real code. Each pointer has point to a char array with a '\0' added at the end implicitly by the compiler.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
9K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
30K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K