Pointers
Each memory location that we use to store the data hase an address in computre memory (RAM). Computer Hardware i.e. CPU uses this addess to reference to a particular data item. A pointer is a variable that stores the address of another variable.
A pointer is a variable that represents the location of a data item, such as a variable or an array element. Pointers are used frequently in C, as they have a number of useful applications. For example, pointers can be used to pass information back and forth between a function and its reference point. Pointers provide a way to return multiple data items from a function via function arguments to
be specified as arguments to a given function.
Pointers are also closely associated with arrays and therefore provide an alternate way to access individual array elements.
Within the computer?s memory, every stored data item occupies one or more adjacent memory cells. The number of memory cells required to store a data item depends on the type of data item. For example, a single character will be stored in 1 byte of memory integer usually requires two adjacent bytes, a floating point number may require four adjacent bytes.
Declaring a Pointer
A pointer is declared just like we declare a variable. There is only one difference in declaration is that we add an asterisk * infront of it to indicate that it’s a variable which is a pointer. For example
int* i; long* y;
you can also declare the pointers as
int *i; long *y;
How Pointer work?
Lets write an example to demonstrate the use of pointers.
int num = 10; int *pnum; pnum = #
Now pnum will be having the address of the num variable.
Initializing pointers
It is often a good practise to intialize the variable while declaring. It is very easy to initialize a pointer to the address of a variable that has already
been defined.
int num=10; int* pnum= #
You can also initialize the pointer to the default null value.
int* pnum = NULL;
Here is a simple example that demonstrates the different aspects of the pointer operations.
#include <stdio.h>
int main(void){
int num=10;
int* pnum=NULL;
pnum = #
*pnum += 20;
printf("\nNumber = %d", num);
printf("\nPointer Number = %d", *pnum);
return 0;
}
Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4]
Tags: Arrays, C Programming, Memory Allocation, Pointer, Variables
There are 25 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.
I ahve declare 5 to 6 pointer arrays and after completing all the operation with them i want to free all the ocupied memory so i am using delete method but it shows error in executing!
Give me the solution for this error
Use p = new T[n] and delete[] p:
Fred* p = new Fred[100];
…
delete[] p;
Any time you allocate an array of objects via new (usually with the [n] in the new expression), you must use [] in the delete statement. This syntax is necessary because there is no syntactic difference between a pointer to a thing and a pointer to an array of things
——– Original Message ——–
I ahve declare 5 to 6 pointer arrays and after completing all the operation with them i want to free all the ocupied memory so i am using delete method but it shows error in executing!
Give me the solution for this error
Explain the above mentioned topics
Please would you giving me the book which deal on
“pointer array, pointer to array, pointer to function”
What is the unique idea of making histogram with the help of c program?
SIR,
I AM STUDENT OF IIIT HYDERABAD. I WANT TELL TO YOU THAT THERE IS SOME SYANTAX ERRORS IS THERE PLEASE YOU CAN MINIMIZES THEM.
THANK YOU.
Hi i want to know know structure variable passed to a function and that function how that structure variable used
plz tell me
I need to know about pointers to string
#include <stdio.h> void sort(int x[], int n); int main(void) { int i; int num[] = {4,5,1,6,8,9,3,2,7,0}; int n = sizeof(num)/sizeof(int); sort(num, n); for(i = 0; i < n; i ) printf("%d", num[i]); putchar(' '); return 0; } void sort(int array[], int n) { int this, next, temp; for(this = 0; this < n; this ) for(next = this 1; next < n; next ) if(array[this] > array[next]) { temp = array[this]; array[this] = array[next]; array[next] = temp; } }change this to pointer declaration.
I want to know how functions work and how they are used ?
Please mail me. humayun0156 at yahoo dot com
Function is a single comprehensive unit that performs a specified task.
Hi, give some idea about pointer to a function.
hi im rakesh from patna university
just translate 1-d array into pointers like:-
a[i] = *(a+i)
and convert the 2d array to pointers like :-
a[i][j]=*(*(a+i)+j)
source code of pointers used in arrays
/*programme to sort number of strings by sorting their locations not their values */ void main() { char a[10][10],*p[10],temp[30]; printf("enter the strings to sort: "); for(i=0;i<7;i++) gets(a[i]); for(i=0;i<7;i++) p[i]=&a[i][0]; for(i=0;i<7;i++) for(j=0;j<(7-i);j++) { if(strcmp(p[j],p[j+1])>0) { strcpy(temp,p[j]); strcpy(p[j],p[j+1]); strcpy(p[j+1],temp); } } printf("the sorted strings are: "); for(i=0;i<7;i++) puts(p[i]); getch(); }Ii want 2 know how can i calculate the prelim midterm ,pre final and final..this the example of formula he give my prof prelim…quiz#1 90×20%,quiz#2 80×10%..attendance 80×20%…then the total of pre lim ,midterm ,pre final and final is calculate …then the final grade is show 2 my c++ program…i hope any one give me example of my program…thx a lot…
Can all of you give me a sample program or source code about pointers used in arrays….plssssss….plsssss…….
Given an expression consisting of operands a, b, c, d…. and operators and *. The expression is fully parenthesized. Write a program to evaluate the expression, given the values of a,b,c,d…
Define a function, char *get_first_subexprn(char *e) which returns the leftmost innermost subexpression ( of the form (operand1 operator operand2) )in e where e is a fully parenthesized expression .
Define a function int evaluate(char * e) which evaluates and returns the value of the expression e. Assume e is of the form (operand1 operator operand2). The operands may be constants or variables.
Your algorithm should repeatedly find the first innermost parenthesized expression and replace it by the value of the sub expression until you get the value of the expression.
This is a question i want to solve….
please help….
I have searched for any topic on sort but was unable to find. however i got a question,
how does one call function to a sort(selection) for instance if you have such a code as;
#include<stdio.h> void select(int number [], int count) { int i, j; int min, temp; for(i=0; i<count-1; i++){ min=i; for(j=i+1; j<count; j++){ if(number[j]<number[min]) min=j; temp=number[i]; number[i]=number [min]; number [min]=temp; } } } main() { int x=0; int num; {'1','5','4','3','6','8','9','0','2','7';} //num=number; for(x=1; x<9; x++) printf("number %d", num); }now the big question is, i seem to be having a problem with calling the function and also with the printf. can sumbody can you help with pointing out what is expected of me. thank u.
Hay Sir
This is very nice way to Undersoot us
Thank u sir
Hi This is Santhosh,
Can any body explain How pointer accessing method is more faster than array indexing?
Regards,
Santhosh
i am having a problem in coming up with the recursive algorithm of the binomial theorem.Could you pliz heelp me.
Hello. I just want to know how to initiate this type of program using pointer. And here is the sample output:
————————————–
Enter Words:Adamson -
-
Letter ‘a’ appears at: 2 -
Letter ‘e’ appears at: 0 -
Letter ‘i’ appears at: 0 –
Letter ‘o’ appears at: 1 –
Letter ‘u’ appears at: 0 -
-
————————————–
Here is my initial program using String and Gets. Its near similar but isn’t a program that i ask:
Please i need your help because i was reviewing for our prelim exam. Thanks and more power.
-rejie
sir plz tell me a program implemaintation of stack using pointer
i like to create a function like strlen.it will count the number of characters in a string…using pointers i would like to know how.tnc
i am watting for pro