29 views3 pages
/*
More about pointers:
-- Pointers are variables whose values are memory addesses
-- Must be defined before they can be used
<data_type> * <pointer_name>;
i.e. int *ptr;
-- ptr is a pointer to an integer or ptr points to an object of type int
-- The * indicates that ptr is a pointer
-- a pointer and the object is pointing to, should have the same data type
i.e. an int pointer can only point to int objects
-- The exception to the rule is void pointers(void pointers can point to
any object type)
-- Pointers should be initialized(store a value in it before is used)
-- when defined or in a subsequent assignment statement
-- A pointer can be initialized to NULL, 0 or an address
-- A pointer with the value NULL points to nothing
NULL is a symbolict constant defined in several header
files(i.e.stdio.h)
-- Initializing to 0 is the same as initializing to NULL( NULL is
prefered)
Because it is initialized to zero, zero has to be first converted to
the
apropiate data type
-- The usual example:
int y=5;
int *yPtr = &y;
--The unary * operator, commonly referred to as the indirection operator or
dereferencing operator, returns the value of the object to which its
operand
points
printf("%d",*yPtr)
"dereferencing a pointer"
-- & and * are complements of one another
printf("%p\n",&*yPtr);
printf("%p\n",*&yPtr);
rigth to left
-- Pointers help to simulate the call by reference
-- Implement linear search to find if a number is part of the array.
--- Returns the index of the key if found, 0 o.w.
--- Receives a pointer to an array of integers
--- An int representing the size of the array
--- A pointer to the value(key)
*/
#include<stdio.h>
void linearSearch(int [], int *, int );
int main()
{
int myArray[] = { 1,2,3,4,5,6,2,45,6,7,8,9 };
int valueIamLookingFor = 45;
int result = linearSearch(myArray, sizeof(myArray) / sizeof(myArray[0]),
&valueIamLookingFor);
if (result != -1) {
printf("The value %d was found at index %d!", valueIamLookingFor,
result);
}
else {
printf("Value %d was not found!", valueIamLookingFor);
}
return 0;
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Document Summary

- pointers are variables whose values are memory addesses. - must be defined before they can be used. - ptr is a pointer to an integer or ptr points to an object of type int. - the * indicates that ptr is a pointer. - a pointer and the object is pointing to, should have the same data type i. e. an int pointer can only point to int objects. - the exception to the rule is void pointers(void pointers can point to any object type) - pointers should be initialized(store a value in it before is used) - when defined or in a subsequent assignment statement. - a pointer can be initialized to null, 0 or an address. - a pointer with the value null points to nothing. Null is a symbolict constant defined in several header files(i. e. stdio. h) - initializing to 0 is the same as initializing to null( null is prefered) the.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions