declare

Working with character arrays and "strings" in C



Sharing buttons:

engineer man here time to learn about

character arrays and see strings no time

to waste let's do this today's video

levels beginner I'm making no

assumptions today with this video so

what is a character array a character

array is exactly what it sounds like

it's an array of characters depending on

how you declare these they can either be

string literals and read-only memory

they could be string literals that

initialize character arrays on the stack

or arbitrary length arrays allocated on

the heap which you can then manipulate

she does not have a string type though

what it does have functions to

manipulate strings the wait a second

that sounds contradictory if there is no

string type then why would there be

functions to manipulate strings well in

C character arrays there's a special

character called a null byte and it's

denoted by a backslash 0 if your

character array has a null byte in it

and in most cases it should that

character array can be thought of kind

of like a string where the string is all

the characters in the character array up

to but not including the null by

character all C string functions that

read manipulate character arrays expect

that null byte to be there and bad

things can happen one is not prison for

instance if you were to call string Len

on a character array without an oblate

it's going to cause it to read the whole

array and then keep reading memory which

it doesn't have access to until it

eventually finds a null byte somewhere

in the memory or the program crashes or

worse let's look at some examples the

first thing is just a single character

it's actually an answered type when you

do char letter and then you do a single

quote what you're saying is give me the

integer version of that letter and that

comes right from the ASCII chart so this

would be actually a 65 you know B would

be 66 C 67 and so on

next one is a string literal and stored

as read-only data when you declare it

like this name one is a point two

character

it takes this string literal stores it

in read-only data and it gives you a

pointer to the first letter in that

string so the e you cannot change this

because it's hard-coded in the actual

read-only memory next one is a character

array initialized from a string literal

copied to and stored on the stack this

is a very similar name to is very

similar to name one except you can edit

this later in your program and there's a

reason for that if we skip down to name

three it's actually identical to name

two what it does is it takes the string

literal which is real only data and then

it uses it to create the following

character array which is stored on the

stack

rather than it read only data therefore

you can manipulate name 3 with no

problem because you never actually touch

and you read only data so if named for

I've created a character array for the

128 elements and it's stored on the

stack

I picked a very large number because I

wanted you to see what happens when you

use a null terminator for the first time

so what I'm doing here on 22 is I'm

copying a string literal engineer man

into name for now string copy will

automatically add a null byte at the end

so it copies 12 bytes into name four and

then it also copies the little

terminator so if I were to print name

for out I would get the full string

engineer man excellent if I call string

length name for it's going to be a 12

because what it's doing is it's counting

up tilde null plate it doesn't matter

that this character array is 128

elements long because remember the

string functions in C only go up to

where the null byte is so in this case

to be 12

as far as accessing elements in

character arrays there's several ways

you can do this primarily it would be

picking the name and then sub scripting

it with an index

and then that would give you that letter

however this is really just an alias in

the format of array bracket index

bracket that's actually expanded to

array plus index which is pointer

arithmetic and that's D referencing that

that pointer so this is going to be

identical to this and because of the way

it does it you now have this weird

syntax you can actually reverse name

four in the index because that's going

to expand to this which is functionally

equivalent to this I don't recommend

using these three unless you have a

special use you can you can stick to

this top one and that's perfectly fine

what this does is that accesses the

element at index zero if you want to

modify it's the exact same thing as

reading it except you provided the new

character and remember it's an Android

type so I'm using single quotes this is

actually a science number whatever the

number is for a dash and then now it's

changed if I were to output name four I

would get engineer

- man and finally if I want to change

the boundary of the string just to show

you that that works I'm I can change

that - from a dash to a null byte and

then if I were to output name four again

I would just get engineer if I were to

do the string length I would just get

eight and that's because it only reads

up until that in Oldboy

null byte and nothing afterwards and

that's it hopefully everyone is smarter

have to watch this video I forgot to

cover something or you'd like to request

a video post a comment below and if you

enjoyed this video

smash that like button and don't forget

to subscribe so you don't miss new

videos see you next time