NO

Author Topic: char arrays ..... Newbies worst enemy  (Read 7844 times)

shazam

  • Guest
char arrays ..... Newbies worst enemy
« on: September 22, 2012, 06:03:56 PM »
Hello all...
I have been trying to teach myself C again and working my way through the book "Head First C"
On chapter 2 we are learning char arrays(and a little about pointers)

I tested the examples and then tried some of my own....Just wanted to ask here to make sure I'm understanding this before I move on in the book

The below code will create a Pointer to a string literal that will be in read only memory(non mutable as the book said)
I tried it and indeed I got a seg fault trying to write to it

Code: [Select]
char *MyString = "Why won't you write to me?";

Also why would anyone create a string literal like that?


ok now the code below will create a char array but still a string literal
Code: [Select]
char AnotherString[] = "This is another string";  

The above code is still read only correct?


The below code creates a char array and is writable (mutable as the book said)

Code: [Select]
char MyMutableArray[256] = "this is a writeble char array";

Just trying to understand how C handles all this and don't want to move forward until I feel I have a better understand of C char arrys

Thank you for taking the time to read my post  :D
« Last Edit: September 22, 2012, 06:21:57 PM by shazam »

CommonTater

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #1 on: September 22, 2012, 08:36:00 PM »
The below code will create a Pointer to a string literal that will be in read only memory(non mutable as the book said)
I tried it and indeed I got a seg fault trying to write to it

Code: [Select]
char *MyString = "Why won't you write to me?";

Also why would anyone create a string literal like that?

There are all kinds of reasons... Error messages, Titles, Help text and so on.  The advantage here is that by making a pointer the text exists only once in memory and the pointer only uses 4 ( or 8 ) bytes of memory on the stack... if you use it to intialize a mutable array, it's in memory twice.

Quote
ok now the code below will create a char array but still a string literal
Code: [Select]
char AnotherString[] = "This is another string";  

The above code is still read only correct?

No it isn't...   What you are actually doing is creating a char array, the length of your initializer. As long as you are mindful of the length you can make changes to this. The advantage is that you can change it, but the disadvantage is that your string now exists twice in memory; once as the constant initializer and again in the array; using the length of the string plus the size of the pointer of space.
 
For example you could use memcpy(AnotherString, "That", 4) and the result would be "That is another string"

Finally if you want to make that immutable you would use const as in const char String[] = {"this is a test"};
 
Quote
The below code creates a char array and is writable (mutable as the book said)

Code: [Select]
char MyMutableArray[256] = "this is a writeble char array";

Yes, this can be changed by any of the library's string functions (check string.h in the help file).

Quote
Just trying to understand how C handles all this and don't want to move forward until I feel I have a better understand of C char arrys

You can safely move forward. 
 
Remember C does not have strings... a char array is just an array, like any other.  A char is simply an 8bit binary number that happens to produce certain recognizeable shapes when sent to the display (Check the ascii chart in the help file). The only thing special about char strings is the terminating null (\0) that library functions count on to know when to stop.
 
 

shazam

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #2 on: September 22, 2012, 09:04:02 PM »
CT,
very much appreciate your feedback

I tried your memcpy example
Code: [Select]
memcpy(AnotherString, "That", 4)
I put the cursor over the function and hit F1 ;) (see you mention that before so it stuck with me)

I understood it  :D

OK going to try a few more projects with char arrays then move on to chapter 2.5 Pointers (Eeeeek!)

Will start another thread about questions on that (but I promise to search the forum and google first)  ;D

Thanks Again!

CommonTater

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #3 on: September 22, 2012, 11:28:03 PM »
OK going to try a few more projects with char arrays then move on to chapter 2.5 Pointers (Eeeeek!)

Pointers are easy ... A pointer is a mailbox number. 
What's in the mailbox is at *pointer ... the * is the key to open it.
If you know the name but not the box number you use &Pointer ... the & gives you the box number.

Feel free to start new threads...

But be sure to try both reading and code examples first.
 
I trust you typed up a bit of code to see my example actually work...
One lesson I learned a long time ago: the stuff you figure out on your own tends to stay with you the best.

 
« Last Edit: September 22, 2012, 11:29:43 PM by CommonTater »

shazam

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #4 on: September 22, 2012, 11:37:18 PM »
Yes CT....
I typed up some code to try (I have been since your response....and before I move on to chapter 2.5)

I'm understanding so far about pointers(the book explains a little before char arrays...then explains char arrays (chapter 2) then in chapter 2.5 they go into Pointers in depth)...just trying to follow the book

I truly appreciate your responses...
I'm not in school....just trying to teach myself programming and C by myself

CommonTater

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #5 on: September 22, 2012, 11:40:54 PM »
I C ... well keep at it :D you'll do fine. 
 
(I am also self-educated... Basic in the late 1970s, Pascal in the mid 1980s, C in the early 2000s, and I've been bouncing off C++ since it came out...)
 
 


 
« Last Edit: September 22, 2012, 11:44:13 PM by CommonTater »

shazam

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #6 on: September 22, 2012, 11:43:02 PM »
Thanks CT  :D

OK onto chapter 2.5...more threads to come!  ;)

CommonTater

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #7 on: September 22, 2012, 11:48:29 PM »
CT,
very much appreciate your feedback

I tried your memcpy example
Code: [Select]
memcpy(AnotherString, "That", 4)
I put the cursor over the function and hit F1 ;) (see you mention that before so it stuck with me)

I understood it  :D

OK going to try a few more projects with char arrays then move on to chapter 2.5 Pointers (Eeeeek!)

Will start another thread about questions on that (but I promise to search the forum and google first)  ;D

Thanks Again!

:D Quicky quiz ...  Can you figure out how to change "this" to "that" by only copying 2 characters?


shazam

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #8 on: September 22, 2012, 11:52:44 PM »
Using memcpy?

CommonTater

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #9 on: September 23, 2012, 12:01:50 AM »
Using memcpy?

That's one method... so, yes, give that a try... Post your code when you're done (or stuck).

There's other ways as well...
 
(Hint... there's a big clue at the bottom of my first message.)


 
« Last Edit: September 23, 2012, 12:05:34 AM by CommonTater »

shazam

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #10 on: September 23, 2012, 12:05:09 AM »
Great!
ok I will create what I think is a solution to this assignment...
Thanks CT  :)

Post back soon

CommonTater

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #11 on: September 23, 2012, 12:06:14 AM »
No need to thank me... I'm having fun :D

shazam

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #12 on: September 23, 2012, 06:59:32 PM »
This is what I used for memcpy

Code: [Select]
int main(void)
{
    char AnotherString[] = "This is another string";
    printf("AnotherString is: %s\n", AnotherString);

   memcpy(AnotherString +2, "at", 2);
printf("AnotherString is now: %s\n", AnotherString);
    return 0;
}


shazam

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #13 on: September 23, 2012, 09:41:21 PM »
This is another way without memcpy

Code: [Select]
int main(void)
{
    char AnotherString[] = "This is another string";

    printf("AnotherString is: %s\n", AnotherString);

    AnotherString[2] = 'a';

    AnotherString[3] = 't';

    printf("AnotherString is now: %s\n", AnotherString);

    return 0;
}


 

CommonTater

  • Guest
Re: char arrays ..... Newbies worst enemy
« Reply #14 on: September 23, 2012, 11:00:15 PM »
Very good... All of those will work just fine...

And there's this way... memcpy(&AnotherString[2], "at",2);