techpreparation-homepage

Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog

  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


Using Pointer Arithmetic With Arrays:
Arrays occupy consecutive memory slots in the computer's memory. This is where pointer arithmetic comes in handy - if you create a pointer to the first element, incrementing it one step will make it point to the next element.

#include <stdio.h>

int main() {
int *ptr;
int arrayInts[10] = {1,2,3,4,5,6,7,8,9,10};

ptr = arrayInts; /* ptr = &arrayInts[0]; is also fine */

printf("The pointer is pointing to the first ");
printf("array element, which is %d.\n", *ptr);
printf("Let's increment it.....\n");

ptr++;

printf("Now it should point to the next element,");
printf(" which is %d.\n", *ptr);
printf("But suppose we point to the 3rd and 4th: %d %d.\n",
*(ptr+1),*(ptr+2));

ptr+=2;

printf("Now skip the next 4 to point to the 8th: %d.\n",
*(ptr+=4));

ptr--;

printf("Did I miss out my lucky number %d?!\n", *(ptr++));
printf("Back to the 8th it is then..... %d.\n", *ptr);

return 0;
}

This will produce following result:

The pointer is pointing to the first array element, which is 1.
Let's increment it.....
Now it should point to the next element, which is 2.
But suppose we point to the 3rd and 4th: 3 4.
Now skip the next 4 to point to the 8th: 8.
Did I miss out my lucky number 7?!
Back to the 8th it is then..... 8.

See more examples on Pointers and Array

Modifying Variables Using Pointers:
You know how to access the value pointed to using the dereference operator, but you can also modify the content of variables. To achieve this, put the dereferenced pointer on the left of the assignment operator, as shown in this example, which uses an array:

#include <stdio.h>

int main() {
char *ptr;
char arrayChars[8] = {'F','r','i','e','n','d','s','\0'};

ptr = arrayChars;

printf("The array reads %s.\n", arrayChars);
printf("Let's change it..... ");

*ptr = 'f'; /* ptr points to the first element */

printf(" now it reads %s.\n", arrayChars);
printf("The 3rd character of the array is %c.\n",
*(ptr+=2));
printf("Let's change it again..... ");

*(ptr - 1) = ' ';

printf("Now it reads %s.\n", arrayChars);
return 0;
}

This will produce following result:

The array reads Friends.
Let's change it..... now it reads friends.
The 3rd character of the array is i.
Let's change it again..... Now it reads f iends.

Generic Pointers: ( void Pointer )
When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer. This is very useful when you want a pointer to point to data of different types at different times.

Try the following code to understand Generic Pointers.

#include <stdio.h>

int main()
{
int i;
char c;
void *the_data;

i = 6;
c = 'a';

the_data = &i;
printf("the_data points to the integer value %d\n",
*(int*) the_data);

the_data = &c;
printf("the_data now points to the character %c\n",
*(char*) the_data);

return 0;
}

NOTE-1 : Here in first print statement, the_data is prefixed by *(int*). This is called type casting in C language. Type is used to caste a variable from one data type to another datatype to make it compatible to the lvalue.

NOTE-2 : lvalue is something which is used to left side of a statement and in which we can assign some value. A constant can't be an lvalue because we can not assign any value in contact. For example x = y, here x is lvalue and y is rvalue.

However, above example will produce following result:

the_data points to the integer value 6
the_data now points to the character a

NEXT >> Using Functions

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