Skip to main content

What is a Pointer in C?

Create a variable
Let's create a variable and give it a value of 3.


int a;

a = 3;

Variable a's info
Namea
Value3

Data Type

The data type of a variable tells you what kind of data it will hold. We can see in the code above that variable a is an int.

int = holds an integer (aka a whole number)
char = holds a single character
pointer = holds a memory address

Variable a's info
Namea
Data Typeint
Value3

Memory Address

The a variable is stored somewhere in your computer's memory. Just like humans have an address, variables have an address too. ( 0x00008130 is just an example address, your variable will probably have a different address)


Pointers

A pointer holds this memory address-- so basically it POINTS to where a variable is located.

Let's create a pointer that will point to where our a variable is located. To create a pointer, we need the data type of the variable we will be pointing to (a is an int) and then a star *. Convention is to put the star next to the variable's name on the right.

int *ptr;

Let's set our new ptr variable to the address of a. To get the address of any variable, we just add & in front of it.

int a;
int *ptr;

a = 3;
ptr = &a;

Variable a's info
Namea
Data Typeint
Value3
Address0x00008130

Variable ptr's info
Nameptr
Data Typeint *
Value0x00008130
Address0x00008500

As you can see, the value of ptr is the address of a. It is POINTING to a.

Using the pointer

You can now use our variable in several different ways.

ptr = gives you the value of ptr. In this case 0x00008130. 

*ptr = gives you the value of the variable ptr is pointing to. In this case, ptr is pointing to the variable located at 0x00008130, which is a. And a's value is 3.
(I remember this as I VALUE you, you're a STAR)

&ptr = as mentioned putting & in front of a variable will give you the address of that variable. In this case we would get 0x00008500
(I remember this as "AND what's your ADDRESS?")

Comments

Popular posts from this blog

How to solve print_bits on the 42 Exam

How to solve print_bits on the 42 Exam This is a walkthrough of how to do the print_bits assignment for the 42 exam. Assignment name : print_bits Expected files : print_bits.c Allowed functions: write -------------------------------------------------------------------------------- Write a function that takes a byte, and prints it in binary WITHOUT A NEWLINE AT THE END. Your function must be declared as follows: void print_bits(unsigned char octet); Example, if you pass 2 to print_bits, it will print "00000010" 1.) Make sure you have a good understanding of bits and bytes with the video above. 2.) Then you need to understand bit shifting. It’s a simple concept and Jamie Dawson has a great short tutorial on it here: Basic Bit Shifting Guide 3.) Understanding Bitwise Operators: I found the Swift guide on bitwise operators to be the best. https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html 4.) Solving Print Bits // solu

CodeCademy Explaination -- Javascript Search Text For Your Name 5/7

First, don't feel bad, this lesson's instructions are kind of confusing. You can skip to what you need if you want. 4 Posts: 1.) Explaining For Loops 2.) Exercise 5/7's if statement 3.) Exercise 5/7's inner loop 4.) Exercise 5/7's push statement 1.)  First you need to understand how  for loops  work. For loops are really useful for doing something over and over.  Example: For every horse in the barn, tell me the horse's name and it's age.  You'll be going to a horse over and over again to get it's name and age. But computers are stupid so you have to be reallyyyy specific about it, like this: For ( every horse in the barn starting at the one in the first stall; until you get to the 12th stall; go to the next stall) { This is what I want you to do at every stall. Get it's name and age.} Now for the computer to understand us we have to speak Javascript to it because it doesn't know English. In Javascript, that's a for loop

Preparing for the 42 Piscine

You can come to the 42 Piscine with no programming experience but if you would like something to study ahead of time here are my 2 suggestions. All this content is free 1.) Learning how to learn on Coursera. This will help you develop the good study habits you will need during the piscine.  https://www.coursera.org/learn/learning-how-to-learn 2.) C Programming Tutorials by Caleb Curry I haven't used this series myself but I have had it recommended a lot, many people have told me it helped them during the piscine so if you'd like to try your hand at C before the piscine you can follow along with these tutorials. https://www.youtube.com/watch?v=CPjZKsUYSXg&list=PL_c9BZzLwBRKKqOc9TJz1pP0ASrxLMtp2 3.)  Khan Academy Brush up on your math skills fore free with Khan Academy. They have both the instructions as well as exercises for practice. Mostly you'll be working with more basic arithmetic for the piscine, but you'll have a few exercises that require more math