From the course: C Standard Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Memory allocation example

Memory allocation example - C Tutorial

From the course: C Standard Library

Start my 1-month free trial

Memory allocation example

- [Instructor] In this example I want to illustrate the importance of allocating memory. So if you take a look at this code in line four we have a definition for a character pointer, that's a string, str. And the main function is very simple. First it goes scanf to read all the characters that come in from the keyword until it finds a new line corrector. That's when someone presses Enter. And lastly, printf just prints that string. This looks very simple doesn't it? Well let's see it working. So let me type in this is so easy. Whoa, notice how the program finished early. I know this because the string wasn't printed out. And look at the return value, this isn't normal. The return value says 3221225477. So what's that big number? Well that's the operating system complaining that scanf was trying to write into some place in the memory it doesn't have any permission to write into. And why would that happen? Well we'll have to look at the code again. So if you take a look at line four…

Contents