How to declare an array of strings in C

Sharing buttons:

all right so how to create declare and

use an array of strings well to find the

answer to this question we first have to

think about what's a string inside C

well Australian sign C is just a just a

an array of characters so an array of

strings would be what simply an array of

an array of characters so

two-dimensional array of characters all

right so I'm gonna go over at least

three ways in which you can instantiate

and use arrays of strings in c so first

thing we have to do for every single

operation we have to include a library

that library is called string dot H

alright this this I really contains some

helpful function for string manipulation

so if you're interested in that you

might want to look at it and do a Google

search for that library and to declare

it all you have to do is say char - say

array and because it's a two-dimensional

array we have to have two pairs of open

and closed brackets and inside each of

them we have to specify a certain size

the first bracket the first pair of

brackets specifies how many strings are

there in our array in our case let's say

three strings mystery that's what I want

in the second pair we have to specify

how many characters each string each

individual string has and we cannot

specify different sizes for every single

string so we have to specify a global

size for every single string in our a

well let's say 15 of these okay that's a

fair number and to

after declaring it to instantiate the

values in this array you can simply use

a function that's called strcpy and

strcpy stands for screen coffee and all

these function does is taking a pointer

to a certain address and it copies a

string of text on top of it and what we

have to do is add in that pointer so in

our case would be a array of 0 and will

be the first string and inside of the

first string in our array we want to

copy this the text hello world let's say

and we have we can do this for every

single element in the array or the glory

of one is a test and strcpy array of 2

is going to be let's say 1 2 3 okay and

the demo say that this works let's also

print something on the screen the

strings are there we go and then we have

to give it all those three pointers to

the string so just just gonna copy paste

this one okay and just you know let's

see if it works and as you can see we

actually printed the right things on the

screen cool

that's that's the first way it's pretty

simple you just have to know how many

strings you want in your array and how

long each string or is the max size of

that string in characters ok a second

way in which you can do this is it's

refusing two pairs of brackets is just

combining them and have just one then

just one one-dimensional array of

characters and have instead of a our

free arrays of 50 characters we can have

three times 50 characters and already

with just three times 50 credits 150

characters okay and now we don't have

access to these anymore a real zero is

just a character now it's no longer a

pointer so we cannot pass in a car a

character what we can do is specify

locations for each of our screen let's

say the first string is at the beginning

of the story

okay so just gonna say already right

nothing more nothing less is just

already the second string though is

found after or 50 captors after the

beginning of the array right so it's

just going to be plus 50 all right

because the first 50 characters are

dedicated for the first or our hello

world here and the second 50 characters

are gonna be dedicated for test and

guess what the third one is going to be

the same you just have to add 100 to it

okay and we can go on forever with this

it's pretty easy and to print them out

instead of typing in array of 0 we just

type in array well for the first one is

just array the second would be array

plus 50 and the third one would be 3

plus 100 and I hope you know a little

bit pointer arithmetic or a plus 50

means go to the address array and move

50 bytes over all right that's that's

what it means in in short but you may

need to look more into it

and if I try to run this we simply get

the three strings down here and the

third way to do this is we're going to

drop this and come back to the double

payout brackets and instead of doing STR

copy like we did here we can already

delete this we can just instantiate it

on the fly

and we can say the or initializer right

so opening closed brackets and in

between doubles we have to specify a

list of strings right so what we can we

have to specify here is what we wanted

to add in those strings of right for our

example just hello world test and one

two three okay and if I come back to the

notation you can already see that we are

using the same we are referencing the

pointers to each string the same way we

did in the first way of instantiating

the strings all right so we have those

three and we just instantiate them on

the fly and it's prettier it's actually

pretty compact can do this in one line

if you really want to apply and it looks

much better you might not even need to

use finish if I try to run this you'll

notice that we get the same result it's

all nice and dandy and in fact one more

cool feature with this instantiation is

if I would like to add another element

disarray and say printed array or or or

m3 you'll notice I have to add one to

this number well you don't really need

to do that anymore you can simply delete

this number because of the array

initializer due to the or initializer

the outermost list or the outermost

array its size is automatically computed

so it's going to just count the number

of elements in here and just drop in

four for us there and we don't have to

do anything so if I try to run this you

will notice that well I am print sorry

you will not say we get all of the

strings on there and just an FYI you

cannot remove this or any other brackets

after the first one if I try to remove

this in fact

and compile it it will say that I don't

know if you can see this but array type

has incomplete element type which means

it doesn't know the size if it has so

only only the outermost array can be

automatically deduced can can have its

size automatically deduced the second

one you have to specify it otherwise

just will say it not bye-bye

all right so make sure you know that and

in fact that that is why you cannot

declare an array of strings using this

notation this notation doesn't really

work it like it it does

compile but doesn't work doesn't do what

we want in fact as you noticed it

already crashed so make sure you use

this notation to avoid for children all

right thanks for watching and I hope you

understood something from this video if

you like these types of videos please

subscribe I'm actually pumping them out

air once everyday it is I'm trying to do

so and I hope they are very helpful

thank you