Skip to content

Programming in C

sofiaariasv2002 edited this page Sep 7, 2024 · 10 revisions

Pointers

Variables: locations in the computer's memory which can be accessed by their identifier (their name). This way, the program does not need to care about the physical address of the data in memory

For a C++ program, the memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address. These single-byte memory cells are ordered in a way that allows data representations larger than one byte to occupy memory cells that have consecutive addresses.

When a variable is declared, the memory needed to store its value is assigned a specific location in memory (its memory address). Generally, C++ programs do not actively decide the exact memory addresses where its variables are stored. image

Address of operator (&)

The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator.

foo = &myvar;

Deference Operator *

Pointers can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*).

myvar = 25;

foo = &myvar;

bar = *foo;

image

Declaring Pointers

Due to the ability of a pointer to directly refer to the value that it points to, a pointer has different properties when it points to a char than when it points to an int or a float. Once dereferenced, the type needs to be known. And for that, the declaration of a pointer needs to include the data type the pointer is going to point to.

The declaration of pointers follows this syntax:

type * name;

image

Structs

Structures are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. image image

Structures Declaration

We specify its member variables along with their datatype.

image

The above syntax is also called a structure template or structure prototype and no memory is allocated to the structure in the declaration. To use structure in our program, we have to define its instance. We can do that by creating variables of the structure type.

image

We can access structure members by using the [( . ) dot operator.] structure_name.member1; strcuture_name.member2;

Initialize structure members

Default Initialization

struct Point { int x; int y; }; struct Point p = {0}; // Both x and y are initialized to 0

Initialization using Assignment Operator

`struct structure_name str;

str.member1 = value1;

str.member2 = value2;

str.member3 = value3;`

###Initialization using Initializer List struct structure_name str = { value1, value2, value3 };

typedef

The typedef keyword is used to define an alias for the already existing datatype. In structures, we have to use the struct keyword along with the structure name to define the variables. Sometimes, this increases the length and complexity of the code. We can use the typedef to define some new shorter name for the structure.

image

Struct vs Union

A union is a user-defined type in which all members share the same memory location. This definition means that at any given time, a union can contain no more than one object from its list of members. image

Clone this wiki locally