0x05. C - Pointers, arrays and strings

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Dynamic data structure

In dynamic data structure, the size is not fixed. It can be randomly updated during the runtime which may be considered efficient concerning the memory (space) complexity of the code. Examples of this data structure are queue, stack, etc.

Data Object

Data Object represents an object having a data.

Linear data structure

Data structure in which data elements are arranged sequentially or linearly, where each element is attached to its previous and next adjacent elements, is called a linear data structure. Examples of linear data structures are array, stack, queue, linked list, etc

Non-linear data structure:

Data structures where data elements are not placed sequentially or linearly are called non-linear data structures. In a non-linear data structure, we can't traverse all the elements in a single run only. Examples of non-linear data structures are trees and graphs.

To determine the size of those types on your computer, you can use

the sizeof operator.

Declaring Arrays In C

type arrayName [ arraySize ]; double balance[10];

To declare an array we use this syntax:

type var_name[number_of_elements]; int t[5]; where number_of_elements is the number of elements of type type that we need.

To get the address where a pointer is stored, you can use the same technique as for any other variable:

use the & operator.

sizeof an array

will give you the amount of memory space used by all its elements.

What are pointers and how to use them

- A pointer is a variable which contains a memory address. - A pointer is simply the address of a piece of data in memory. General form is: var_type *var; The * tells that the variable var is a pointer...

p = &n;

& takes the address of n. So now p == &n, so *p == n

int *p;

* is used in the declaration: p is a pointer to an integer, and so, after dereferencing, *p is an integer.

Linear data structure types

1- Static data structure: 2- Dynamic data structure:

the memory layout of C program contains five segments these are the

1- stack segment, 2- heap segment, 3- BSS (block started by symbol), 4- DS (Data Segment) 5- text segment.

a char variable could only hold

256 (2^8, 8 being the number of bits) different values: from -128 to 127.

int variable can hold

2^32 different possible values

What is a data structure?

A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently.

Data Definition

Atomic − Definition should define a single concept. Traceable − Definition should be able to be mapped to some data element. Accurate − Definition should be unambiguous. Clear and Concise − Definition should be understandable.

In C, an array is

NOT a pointer

Static data structure:

Static data structure has a fixed memory size. It is easier to access the elements in a static data structure. An example of this data structure is an array.

Basic Operations

The data in the data structures are processed by certain operations. The particular data structure chosen largely depends on the frequency of the operation that needs to be performed on the data structure. - Traversing - Searching - Insertion - Deletion - Sorting - Merging

Built-in Data Type

Those data types for which a language has built-in support are known as Built-in Data types. For example, most of the languages provide the following built-in data types. - Integers - Boolean (true, false) - Floating (Decimal numbers) - Character and Strings

Derived Data Type

Those data types which are implementation independent as they can be implemented in one or the other way are known as derived data types. These data types are normally built by the combination of primary or built-in data types and associated operations on them. For example − - List - Array - Stack - Queue

Arrays in C

are contiguous memory areas that hold a number of values of the same type.

sizes of the most common types on most 64-bit Linux machines:

char -> 1 byte int -> 4 bytes float -> 4 bytes

To do dereferencing, you can use the

dereference operator *.

The real power of pointers is that they can manipulate values stored at the memory address they point to. This is called

dereferencing. الاشتقاق

Initializing Arrays in C

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

When the name of an array is used in an expression, the array type

gets automatically implicitly converted to pointer-to-element type in almost all contexts (this is often referred to as "array type decay").

The size of a type will determine

how many different possible values a variable of this type can hold.

A segmentation fault

is a common problem that causes programs to crash. A core file (core dumped file) also associated with a segmentation fault that is used by the developer to finding the root cause of the crashing (segmentation fault).

Data Type

is a way to classify various types of data such as integer, string, etc. which determines the values that can be used with the corresponding type of data, the type of operations that can be performed on the corresponding type of data. There are two data types − Built-in Data Type Derived Data Type

The real power of pointers

is that they can manipulate values stored at the memory address they point to

You can use %p to

print addresses (the values of pointers) with printf

to know what is the address of a variable, you can use

the "address-of unary operator" &. - ex : printf("Address of variable 'c': %p\n", &c); - Address of variable 'c': 0x7ffc370ef13b


संबंधित स्टडी सेट्स

Chapter 01 The Core Principles of Economics

View Set

Exam 2: Election of 1800 and its significance

View Set

Chapter 3: Taxes, retirement, etc.

View Set

Unit2 Prokaryotic and Eukaryotic Cells

View Set

Bonds and Other investments Quiz

View Set