𝐏𝐨𝐢𝐧𝐭𝐞𝐫𝐬 & Tutorial 13

 


What is Pointer?

  • A pointer is a variable that contains the memory address of another variable, where the real data is stored.

Declaring Pointing variable

Syntax of declaring pointer variable
                    data_type *pointer_variable_name;
                    int *ptr;

There are three position for valid the "*" mark . * As known as Indirection Operator.
                    int *ptr;
        int * ptr;
        int* ptr;

Initializing Pointers

Once a pointer is declared, the programmer must initialize the pointer, that is, make the pointer point to something. A pointer variable can be initialize as
follows;
                   pointer_variable_name = &variable;


Tutorial 13

Given the following declarations:

char *message = "Programming in C is fun\n"; 
char message2[] = "You said it\n"; 
char *format = "x = %i\n"; int x = 100;   

Determine whether each printf call from the following sets is valid and produces the same output as other calls from the set. 

/*** set 1 ***/ 
printf ("Programming in C is fun\n"); 
printf ("%s", "Programming in C is fun\n"); 
printf ("%s", message); printf (message); 
  • Output get successfully. Valid and produces the same output.
  • Output is Programming in C is fun print 4 times in down.

/*** set 2 ***/ 
printf ("You said it\n"); 
printf ("%s", message2); 
printf (message2); 
printf ("%s", &message2[0]); 
  • Output get successfully. Valid and produces the same output.
  • Output is You said it print 4 times in down.

/*** set 3 ***/ 
printf ("said it\n"); 
printf (message2 + 4); 
printf ("%s", message2 + 4); 
printf ("%s", &message2[4]); 
  • Output get successfully. Valid and produces the same output.
  • Output is said it print 4 times in down.

/*** set 4 ***/ 
printf ("x = %i\n", x); 
printf (format, x);  
  • Output get successfully. Valid and produces the same output.
  • Output is x = 100 print 2 times in down.

Write a program in C to find the maximum number between two numbers using a pointer.

#include<stdio.h>
int main(){
int *num1, *num2, x, y, max;
printf("Enter two numbers ");
scanf("%i %i", &x, &y);
num1=&x;
num2=&y;
max=*num1>*num2?*num1:*num2;
printf("Maximum %i\n", max);
return 0;
}

Write a program in C to store n elements in an array and print the elements using pointer.

#include<stdio.h>
int  main(){

int numbers[]={0,1,2,3,4,5,6,7,8,9};
int *numPtr=NULL, i;
numPtr=numbers;
for(i=0;i<10;++i){
printf("numbers[%i] = %i\n", i, *numPtr);
numPtr++;
}

return 0;
}

Write a program in C to calculate the length of the string using a pointer. 

With using function.
#include<stdio.h>

int lengthOfString(char *string);

int  main(){

char word[1000];
printf("Enter the Word : ");
scanf("%s", &word);
printf("There are %i characters\n", lengthOfString(word));   
return 0;
}

int lengthOfString(char *string)
{
int length=0;
while(*string)
{
length++;
*string++;
}  
return length;
}

Without using function
#include<stdio.h>
int  main(){

char word[1000];
char *string;
int i, count=0;
printf("Enter the Word : ");
scanf("%s", &word);
string=word;
for(i=0;i<1000;++i)
{  
if(*string=='\0')
break;
count++;
}
printf("There are %i characters\n", count);   
return 0;
}

Write a program in C to find the factorial of a given number using pointers.    

#include<stdio.h>
int  main(){

int i, number, *fac, x=1;
printf("Enter Number : ");
scanf("%i", &number);
fac=&x;
for(i=number;i>0;--i){
*fac*=i;
}
printf("%i! = %i\n", number, *fac);
return 0;
}

Write a program in C to count the number of vowels and consonants in a string using a pointer.  

#include<stdio.h>
int  main(){

char word[100];
char *string;
int i, j, vCount=0, cCount=0;
char vowels[]={'A','E','I','O','U','a','e','i','o','u'};
printf("Enter the Word : ");
scanf("%s", &word);
string=word;
for(i=0;i<100;++i)
{
if(*string=='\0')
break;
for(j=0;j<10;++j){
if(vowels[j]==*string){
vCount++;
}  
}
cCount++;
*string++;
  
}
printf("There are %i Vowels and %i consonants\n", vCount, (cCount-vCount));   
return 0;
}

Comments

Popular posts from this blog

Programming Errors in C

Simple and basic C program code

printf() Function