[go: nahoru, domu]

Jump to content

User:Josephbubb/C (programming language)

From Wikipedia, the free encyclopedia

Data types[edit][edit]

Pointers[edit][edit]

C supports the use of pointers, a type of reference that records the address or location of an object or function in memory. Pointers can be dereferenced to access data stored at the address pointed to, or to invoke a pointed-to function. Pointers can be manipulated using assignment or pointer arithmetic. The run-time representation of a pointer value is typically a raw memory address (perhaps augmented by an offset-within-word field), but since a pointer's type includes the type of the thing pointed to, expressions including pointers can be type-checked at compile time. Pointer arithmetic is automatically scaled by the size of the pointed-to data type. Pointers are used for many purposes in C. Text strings are commonly manipulated using pointers into arrays of characters. Dynamic memory allocation is performed using pointers. Many data types, such as trees, are commonly implemented as dynamically allocated struct objects linked together using pointers. Pointers to functions are useful for passing functions as arguments to higher-order functions (such as qsort or bsearch) or as callbacks to be invoked by event handlers.

A null pointer value explicitly points to no valid location. Dereferencing a null pointer value is undefined, often resulting in a segmentation fault. Null pointer values are useful for indicating special cases such as no "next" pointer in the final node of a linked list, or as an error indication from functions returning pointers. In appropriate contexts in source code, such as for assigning to a pointer variable, a null pointer constant can be written as 0, with or without explicit casting to a pointer type, or as the NULL macro defined by several standard headers. In conditional contexts, null pointer values evaluate to false, while all other pointer values evaluate to true.

Void pointers (void *) point to objects of unspecified type, and can therefore be used as "generic" data pointers. Since the size and type of the pointed-to object is not known, void pointers cannot be dereferenced, nor is pointer arithmetic on them allowed, although they can easily be (and in many contexts implicitly are) converted to and from any other object pointer type.

Careless use of pointers is potentially dangerous. Because they are typically unchecked, a pointer variable can be made to point to any arbitrary location, which can cause undesirable effects. Although properly used pointers point to safe places, they can be made to point to unsafe places by using invalid pointer arithmetic; the objects they point to may continue to be used after deallocation (dangling pointers); they may be used without having been initialized (wild pointers); or they may be directly assigned an unsafe value using a cast, union, or through another corrupt pointer. In general, C is permissive in allowing manipulation of and conversion between pointer types, although compilers typically provide options for various levels of checking. Some other programming languages address these problems by using more restrictive reference types.

Although careless use of pointers may have consequences that are hard to find such as memory leaks and segmentation faults, pointers are often used to write more efficient programs or when the programmer would like multiple variables to reference one spot in memory. For example, a struct is a data type which allows the programmer to define a collection of variables under one name[1], and the memory allocated to the struct is the sum of the sizes of the variables contained within the struct. Therefore, when a function takes a struct as an input, the entirety of the struct is duplicated for that scope. To save memory, a function may instead take a pointer to a struct as an input, which allows variables to be accessed by using the arrow operator.

The syntax for using the arrow operator is:

#include <stdio.h>

typedef struct person {
    char *name;
    int age;
}

void print(person *p) {
    printf("%s is %d years old.", p->name, p->age);
}
  1. ^ Olsson, Mikael (2019). Modern C quick syntax reference : a pocket guide to the language, APIs, and library (Second edition ed.). New York, NY. ISBN 978-1-4842-4288-9. OCLC 1080520557. {{cite book}}: |edition= has extra text (help)CS1 maint: location missing publisher (link)