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...

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 ...

Resources for getting a job as a software developer.

Preparation 0.) Elevate Code Interview Prep + Job Tracker Sheet I've created a google sheet that is great for job hunting. Track data about which jobs you've applied for and fill out the common questions with your answers so you don't have to think about what to say during the interview. https://docs.google.com/spreadsheets/d/1cVSuABWR1dwaF5j4AZ46grS9ggl37NaBY5X1bK2ktt0/edit?usp=sharing  1.) Github Git is the most widely used source control. Showing that you know how to use it and having a portfolio of code that employers can look at can give your career a boost. 2.) Leetcode Great for practicing in a style very similar to interviews, plus the discussion forums can show you a better way to solve things. There's a lot of great tutorials here as well. I highly suggest also posting your answers in a repo on Github. Don't try to memorize problems, but use it as a way to test your knowledge of applying the fundamentals. 3.) HackerRank Similar to Leetcode, bu...