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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top