Udemy - Advanced c programming pointers - Addresses and Indirection What is the relationship between a pointer and an array (or string)? Transcription : Arrays in C are actually just sequential data items stored at some location in memory the address of the array is the same as the address of the first item in the array because it is where the array begins. The name of the array that is the name you get to the array variable is also the address of the array. To explain this let's look at how strings are used in C unlike many other languages. C does not have a dedicated string data type in C A string is just an array of characters for convenience C treats the null character Slash's zero as the string Terminator. When you declare a string variable like this C automatically ODs a null char at the end. Now let's see if we can find where this string is stored in memory. Now here's my program and here you can see that I've just created a string hello world whi...
Udemy - advanced c programming pointers - Pointer Basics
What is this course about ?
Transcription :
Welcome to this c course on pointer's before we get started let me say a bit about who this course is for what you get out of it. Now let me be clear I'm assuming you already have at least some experience of programming in C before you start this course. This is definitely not a course for beginners. You need to know at least the basics of C programming syntax how to write functions parse arguments how to edit and compile C programs. You may also have at least some experience of using pointers but that isn't essential if you are completely new to see where I already have another course on C programming for beginners. And that's why you should start this course is more advanced. It explains pointers in depth in the early stages I'll explain what pointers are and how to use them. I'd explain addresses and in direction allocating and freeing memory pointer arithmetic and using pointers to arrays and array elements and then go on to explain some of the more mysterious features of pointers and memory. Why a struct can sometimes take up more memory than the data it contains. And why not cause problems when allocating memory. How do you create linked lists with single or double links and how to add and delete list elements from the end of a list or from somewhere in the middle. I'll explain how to use punter's with specialist types such as stocks and Qs. How to use pointers to functions or even how to use an array of function pointers. And finally look at common problems that you experience with pointers memory leaks dereferencing null pointers pointers out of scope errors to do with type casting and problems when making deep and shallow copies. If you've ever struggled to understand pointers or how to use them effectively and safely in your own programs this is the code for you. It covers a lot of ground and I have to admit some topics may be quite hard to grasp. However if you watch all the lessons and use the sample source code that I provide by the time you've completed this course you will have a deep and solid understanding of pointers in s.p.
How to use this course ?
Transcription :
The core learning materials in this course are the video lessons.There are some supplementary materials too.Most important of these is the source code archive. The archive includes all the source code of all the examples shown in the videos. You don't have to use code is quite possible that the video lessons will teach you everything you need to know. However I would suggest that you download and unzip the archive and at least take a closer look at the code of any subjects that you find while complicated or difficult. You can load the code into any C editor or ID and you should be able to run it with pretty much any standard C compiler on any major operating system. I've provided the code as Hughie's of projects that can be loaded into the free code Light editor. If you want the simplest fastest way to load and run the code well you may want to install code right and you can download a copy from code right. Dot org However this is an advanced level course I'm assuming that you're already programming C. Well you probably have your own favorite C editor that's no problem. Just create an NTC project in whatever editor you like then load my code into a text editor and copy and paste it into your C project. Incidentally the programs contain a lot of comments. So again if you need some clarification of exactly how the code works. Reading the comments will probably be helpful if you're already a fairly experienced programmer. You might want to skip the early lessons and go straight to some of the more advanced topics. However bear in mind that even the early lessons helped to explain some essential basics of pointers. Many books and tutorials on C tend to be quite vague on what the punter there really is. They show pointers as arrows pointing to memory locations but without explaining what those arrows are or how appointer knows where to point and it is vital to understand that these arrows were laid just diagrams. They don't really show what a pointer is or how it works. Diagrams with heroes are sometimes useful undefined later in this course I'll occasionally use them myself but they don't truly represent what pointers really are pointers. Just a bit of data I value a number. You really need to understand that at the outset only once you've understood what a pointer is. Well the diagrams with all the arrows pointing to things start to make some sort of sense. OK so let's stop.
ReadMeAndFAQ :
Pointer source code for the exercises
The source code of all the examples shown in this course can be downloaded and unzipped from this archive.
What is a pointer?
Transcription :
Before we go any further you need to be sure that you understand exactly what a pointer is in many languages such as Java Ruby C-Sharp and others. You either never come across pointers or Tor or only in very special circumstances behind the scenes. All those languages use pointers but they hide them from the programmer. And that's because pointers can be quite hard to use and if you use them incorrectly they can cause all sorts of errors. In C on the other hand there's no getting away from pointers. That's because C operates at a relatively low level by low level. I mean it's a language that makes it fairly easy to access the operating system and the computer hardware. On the plus side that means that you can optimize your programs to make them very fast and very efficient. On the downside it means that C does not protect your programs from making silly or even dangerous mistakes. And one of the commonest causes of programmer errors in C pointers so let's be clear what exactly is a pointer. Put simply a pointer is a variable that refers to all points to an address in your computer's memory. Ok so now you need to be sure that you understand what Watson addresses. Think of the computer memory as a long series of locations where bits of data stored on the location or position in memory where some piece of data is stored is an address. So if you want to find a specific piece of data in memory you need to find the address where it's stored. And that's what a point does. If you find that hard to visualize try thinking how you'd get something that's been stored somewhere in the real world. Imagine a long road with lots of warehouses on it. Each warehouse contains an artifact. I know that a specific artifact has been stored in a specific warehouse and I want you to get it for me. But how are you going to locate the warehouse on a road where there are hundreds thousands or even millions of different warehouses. Well of course I need to give you the address and here it has so I've written the address Warehouse 13 on this index card. Now you have the address so you can go to the correct location. And in computer terms this card is like a pointer variable. Its value is an address by using the pointer to locate the address. That is a precise location in computer memory. I can access data stored at that location or alternatively I can store a new piece of data at that location. And the next lesson will see how you would do this in a C program.
Pointer Variable
Transcription :
Now let's see how we can create and use a pointer variable in C here. I've declared a simple interfered called number and I've declared a pointer variable the asterisk store operator shows that this is a pointer the data type shows the type of data to which this pointer will point. So this pointer will point to a memory location containing an integer. Incidentally it's up to you to put the asterisk when declaring the pointer. Some programmers put it immediately after the data type in star. Others put it before the variable name. So I could do this. Leave a space after int and then put Stanham PDR. So whichever you do they work the saying as long as the asterisk is placed between the type and the variable name. The declaration is valid here here I assign an interval you that's 100 into variable name and I set the point available. PDR to point to the NUM variable. I do that using the addressable operator. The ampersand in front of the NUM variable that gets the address. Remember that's the memory location of the variable under the signs but address to the NUM PTL variable. Let's be clear on this. The address of operator does not return the value of the NUM variable. That is 100. It returns the address in memory at which that value is stored. And if I run the program we should be able to see that this is my output. So here you can see that the value of name variable is as expected 100. But the value of the point available number PDR is this long. Here here's a what is that number. Well let's try using the address of operator and we just close this window first. Use the address of operator here. So I put in front of num here so when we run the code this time we're not going to see the value which was 100. We see the address of the variable and do it now. Start here again. So this now displays the address of num. Or we can see that the value of PTO or is the address of the variable name. Those two numbers are the same but of course just having an address of a variable is not likely to be all that useful unless we can also find out what data is stored at that address and in the next lesson. I'll show you how to access data.
Indirection
Transcription :
Let's see how I can find the value of the integer to which my pointer is pointing. In fact this variable number 2 does exactly that. I want to assign to them to the value stored at the address referenced by the pointer variable number TTR remember PTA points to the address ampersand dom of the NUM variable and that variable has the value 100. Here I once again use the store operator that has an asterisk. And when I put this in front of a pointer variable it returns not the address but the data that is found at that address and I can now assign that data to my individual number two. And when I run my program let's do that now. So you can see now that I can maybe have to verify that number two has indeed been assigned the value of was 100 to the value 100. So in summary the pointer PDR gave me the address of the NUM variable and the store operator returned the data stored at that address accessing data stored at an address in this way is sometimes known as dereferencing or in direction pointer. Gives me a reference to an address and the asterisk which is sometimes called the dereferencing operator or the indirection operator gives me access to the data it's stored at that address.
Comments
Post a Comment