Passing an Array to a Function | C Programming Tutorial

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Huge appreciation, I feel the effort to explain the topic, because you know it's confusing and it needs more attention and extra explanation. Thank you a lot. If I meet in this life: I will give you a BIG HUG

naboulsikhalid
Автор

I'm lucky enough to have come to C from assembler, so I intuitively know about local variables being on the stack, and parameters passed through registers. I hope modern teachers make this clear. Funny enough, I learned this as a hobbyist as a kid before going to university to learn computer science

mikehibbett
Автор

Very useful video btw..
Whenever I get stuck with the cs50 course, i come running to your videos, gain clarity and then rewatch the cs50 content to cement the idea..
If i ever make a how to learn code video, im going to include your channel in it

SketchupGuru
Автор

Thank you for this amazing explanation! 🤩

hazemba
Автор

Incredible stuff. Thank you so so much!!!

flywittzbeats
Автор

another way to prove it is with the sizeof operator, if you use sizeof on an actual array it will return the number of bytes of the array, if you use sizeof on an array that is a function parameter, it will return 4 or 8 (size of a pointer)

Brad_Script
Автор

Nice and clear explanation. Thank you.

yunustan
Автор

If you have to pass an array to a function, it would be best (if you're not using a struct wrapper) to use
foo(size_t size, T array[static size]);
to communicate your intentions to whoever is reading your code, as well as to provide the compiler additional info.
For passing a pointer to a single value, use
foo(T ptr[static 1]);
which will allow the compiler to issue warnings if NULL is passed to this function.
Iirc, 'T ptr[static N]' means that ptr points to at least N valid objects of type T.

john.darksoul
Автор

So effectively, array as argument in function is really passed by reference.

LeeHongYee
Автор

I'm assuming it's going to be the same for strings. We pass memory addresses aka pointers in string as well?

SketchupGuru
Автор

Good day, Mr. Teacher.
I got 2 questions: in your video example with arrays how can I a star pointer "*" work without an ampersand "&"? This is the second time I've seen it.

First one was this:
int main()
{
char str[] = "All the world's a stage, and all the men and women merely players";
int size = strlen(str); //determining string's length
printf("Old line: \n");

//print the first word
char sep [10]=" ";
char* words= strtok (str, sep); //why is star required here?
printf("The first word is: %s \n", words);

return 0;
}

Why do we need to use "*" after char and how come does it work w/o an ampersand? Hope you'll help =)

Acksakal
Автор

What if you return x; in the add function?
Wouldn't that be carried to the main function

SketchupGuru
Автор

And of course that pointer is to a variable on the stack, so it wont be around for long :)

mikehibbett
Автор

What is the editor you are using ? I can see the name is Code ? But when I look for Code for Mac, I only get Visual Studio Code results

ddlc
Автор

so i have a question. when i print out the array by itself by using the %p specifier i get a memory address being pointed by the array. and when i use the %p in conjuction with &array i get the memory address of where the array variable is stored. then i use the %p for array[0] and i get yet another address. i thought the address pointed by array would be the first element of the array but it is not? this raises the question of why is there an intermediate address between the value stored in array (address) and the first element of array (address). sorry for the long question! Love your videos <3

franciscojusto
Автор

what if i want to make operations at all array elements. should i use for in the function ???

mahmoudelhanafy