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
AI Thread Summary
The discussion revolves around the correct understanding of a char pointer array in C programming. It clarifies that the declaration `char *strings[4]` creates an array of four pointers to char, not an array of four character cells. The confusion arises from interpreting string literals like "Monday morning" as addresses; they are indeed pointers to character arrays. The importance of the null terminator (`\0`) in character arrays is emphasized, noting that each string literal automatically includes this terminator. The incorrect assertion that the fourth cell should contain a string instead of a null terminator is addressed, reinforcing that the compiler handles the null terminator for string literals. Overall, the conversation highlights the nuances of pointer usage and string representation in C.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top