|
C Programming Tutorials
Basics of C:
Facts about C
Why to Use C
C Program File
C Compilers
Program Structure:
Simple C Program
C
Program Compilation
Basic DataTypes:
DataTypes
Modifiers
Qualifiers
Arrays
Variable Types:
Local Variable
Global
Variable
Storage Classes:
auto storage class
register storage
class
static storage
class
extern storage
class
Using Constants:
Defining Constants
The enum Data Types
Operator Types:
Arithmetic Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc
Operators
Control Statements:
Branching
Looping
Input and Output:
printf() function
scanf() function
Pointing to Data:
Pointers and Arrays
Pointer
Arithmetic
Pointer Arithmetic
with arrays
Functions:
Using functions
Declaration and
Definition
Strings:
Reading and Writing
Strings
String Manipulation Function
Structured DataTypes:
Structure
Pointer to Structure
Working with Files:
Files
Basic I/O
Bits:
Bits Manipulation
Bits Field
Pre-Processors:
Pre-Processors Examples
Parameterized Macros
Macro
Caveats
Useful Concepts
Built-in Library Functions:
String Manipulation
Function
Memory Management
Function
Buffer
Manipulation
Character
Functions
Error Handling
Functions
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
C Programming Tutorials
Pointing to Data
A pointer is a special kind of variable. Pointers are designed for storing
memory address i.e. the address of another variable. Declaring a pointer is the
same as declaring a normal variable except you stick an asterisk '*' in front of
the variables identifier.
-
There are two new operators you will need to know to work
with pointers. The "address of" operator '&' and the "dereferencing"
operator '*'. Both are prefix unary operators.
-
When you place an ampersand in front of a variable you
will get it's address, this can be stored in a pointer variable.
-
When you place an asterisk in front of a pointer you will
get the value at the memory address pointed to.
Here is an example to understand what I have stated above.
#include <stdio.h>
int main()
{
int my_variable = 6, other_variable = 10;
int *my_pointer;
printf("the address of my_variable is : %p\n", &my_variable);
printf("the address of other_variable is : %p\n", &other_variable);
my_pointer = &my_variable;
printf("\nafter \"my_pointer = &my_variable\":\n");
printf("\tthe value of my_pointer is %p\n", my_pointer);
printf("\tthe value at that address is %d\n", *my_pointer);
my_pointer = &other_variable;
printf("\nafter \"my_pointer = &other_variable\":\n");
printf("\tthe value of my_pointer is %p\n", my_pointer);
printf("\tthe value at that address is %d\n", *my_pointer);
return 0;
} |
This will produce following result.
the address of my_variable is : 0xbfffdac4
the address of other_variable is : 0xbfffdac0
after "my_pointer = &my_variable":
the value of my_pointer is 0xbfffdac4
the value at that address is 6
after "my_pointer = &other_variable":
the value of my_pointer is 0xbfffdac0
the value at that address is 10 |
Pointers and Arrays
The most frequent use of pointers in C is for walking efficiently along arrays.
In fact, in the implementation of an array, the array name represents the
address of the zeroth element of the array, so you can't use it on the left side
of an expression. For example:
y is of type pointer to character (although it doesn't yet
point anywhere). We can make y point to an element of x by either of
Since x is the address of x[0] this is legal and consistent.
Now `*y' gives x[0]. More importantly notice the following:
*(y+1) gives x[1]
*(y+i) gives x[i]
and the sequence
y = &x[0];
y++;
leaves y pointing at x[1]. |
Pointer Arithmetic:
C is one of the few languages that allows pointer arithmetic. In other
words, you actually move the pointer reference by an arithmetic operation. For
example:
int x = 5, *ip = &x;
ip++; |
On a typical 32-bit machine, *ip would be pointing to 5 after
initialization. But ip++; increments the pointer 32-bits or 4-bytes. So whatever
was in the next 4-bytes, *ip would be pointing at it.
Pointer arithmetic is very useful when dealing with arrays, because arrays and
pointers share a special relationship in C.
NEXT >> Using
Pointer Arithmetic with Arrays
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers
|