Showing posts with label Pointer. Show all posts
Showing posts with label Pointer. Show all posts

C Program Pointer Example

How to write a C Program Pointer Example in C Programming Language ?


Solution For C Program :

C Program To Reverse The String Using Pointer

How to write a C Program To Reverse The String Using Pointer in C Programming Language ?


Solution For C Program :

/*C Program To Reverse The String Using Pointer.*/


#include<stdio.h>
#include<conio.h>
void reverse(char *);
void main()
{
clrscr();
  char st[10];
  gets(st);
  printf("reverse=");
  reverse(st);
  printf("\noriginal data=%s",st);
  getch();
}
void reverse(char *s)
{
  char c;
  if (*s==NULL)
  {
     return;
  }
  c=*s;
  s++;
  reverse(s);
  printf("%c",c);
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable



C Program to Find max and min in array using pointer concept

How to write a C Program to Find max and min in array using pointer concept in C Programming Language ?


This C Program to Find maximum and minimum in array using pointer concept.

Solution:

  1. /*C Program to find maximum and minimum element in array using pointer */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void max_min(int num[],int length,int *max,int *min);
  6.  
  7. int main()
  8. {
  9.     int len;
  10.     int num[len];
  11.     int i,max,min;
  12.     printf("How many numbers do you wanna input ?");
  13.     scanf("%d",&len);
  14.     printf("Enter %d numbers to find the biggest and small (enter x to terminate)",len);
  15.     for(i=0;i<len-1;i++)
  16.     {
  17.         scanf("%d",&num[i]);
  18.     }
  19.  
  20.     max_min(num,len,&max,&min);
  21.  
  22.     printf("Biggest number :%d",max);
  23.     printf("Smallest number :%d",min);
  24.  
  25.     system("pause");
  26.     return 0;
  27. }
  28.  
  29. void max_min(int num[],int length,int *max,int *min)
  30. {
  31.     int k;
  32.     for(k=0;k<length-1;k++)
  33.     {
  34.         if(num[k]>*max)
  35.             *max=num[k];
  36.             else if(num[k]<*min)
  37.                 *min=num[k];
  38.     }
  39. }

C Program to Adds literal to list of literals

How to write a C Program to Adds literal to list of literals in C Programming Language ?

Adds literal to list of literals.
Param token Token which contains literal value.
Return Pointer to the list of literals on current literal.

  1. /**
  2.  *
  3.  * Adds literal to list of literals.
  4.  *
  5.  * \param token Token which contains literal value.
  6.  *
  7.  * \return Pointer to the list of literals on current literal.
  8.  *
  9.  */
  10. static void* add_literal(token_t* token) {
  11.  
  12.         void* literal;
  13.  
  14.         if (token -> lexeme == T_DOUBLE)
  15.                 literal = get_literal_double(literals, token -> value -> double_value);
  16.  
  17.         else if (token -> lexeme == T_INT)
  18.                 literal = get_literal_int(literals, token -> value -> int_value);
  19.  
  20.         else if (token -> lexeme == T_STRING)
  21.                 literal = get_literal_string(literals, &token -> value -> string_value);
  22.  
  23.         else
  24.                 return NULL;
  25.  
  26. }

C Program to find smallest in an array using pointer

How to write a C Program to find smallest in an array using pointer in C Programming Language ?


This C Program to find smallest in an array using pointer.

Solution:

  1. #include<stdio.h>
  2. main()
  3. {
  4.         int i,n;
  5.         printf("\nHow many elements?: ");
  6.         scanf("%d",&n);
  7.         int a[n];
  8.         for(i=0;i<n;i++)
  9.         {       printf("Element %d: ",i+1);
  10.                 scanf("%d",&a[i]);
  11.         }
  12.         printf("\nCreating pointer now...");
  13.         int *p,small;
  14.         p = a;
  15.         small=*p;
  16.         i=1;
  17.         while(i<n)
  18.         {
  19.                 if(small > *p)
  20.                         small=*p;
  21.                 i++;
  22.                 p++;
  23.         }
  24.         printf("\nSmallest number: %d \n",small);
  25. }

C Program Simple linked list

How to write a C Program Simple Linked List in C Programming Language ?


The following program shows how a simple, linear linked list can be constructed in C, using dynamic memory allocation and pointers.

Solution 1:
#include<stdlib.h>
#include<stdio.h>

struct list_el {
   int val;
   struct list_el * next;
};

typedef struct list_el item;

void main() {
   item * curr, * head;
   int i;

   head = NULL;

   for(i=1;i<=10;i++) {
      curr = (item *)malloc(sizeof(item));
      curr->val = i;
      curr->next  = head;
      head = curr;
   }

   curr = head;

   while(curr) {
      printf("%d\n", curr->val);
      curr = curr->next ;
   }


Solution 2 :
/*simple linked list*/
#include <stdio.h>

int main(void)
{
    struct entry
    {
        int value;
        struct entry* next;
    }n1, n2, n3, *list_pointer;
     
    n1.value = 100;
    n1.next = &n2;
 
    n2.value = 200;
    n2.next = &n3;
 
    n3.value = 300;
    n3.next = (struct entry *) 0;
 
    list_pointer = &n1;
 
    while (list_pointer !=n3.next)
    {
        printf("%i\n", list_pointer->value);
        list_pointer = list_pointer->next;
    }
    return 0;
}

File Handling (console to file) in C Program

How to write a C Program Writing a character into a file,reading the character from the console and writing the next letter into the file in C Programming Language ?



Solution:
/* Writing a character into a file,reading the character from the console and writing the next letter into the file*/

#include<stdio.h>
#include<stdlib.h>

int main()
{

FILE *fp; //creates a file pointer
char ch;
int con;


fp=fopen("filehandling.txt","w+"); // creates a text file with label "filehandling" in write mode

if(fp==NULL)  //checks for the filename validity
{
printf("\nUnable to open the file\n");
exit(0);
}
printf("\nFile opened successfully\n");

printf("\nEnter the character as input\n");
scanf("%c",&ch);


putc(ch,fp);

fclose(fp);

fp=fopen("filehandling.txt","rwa+");

ch=getc(fp);


con=ch;
con++;
if(con>=97 && con<122)
{

fprintf(fp,"\nthe next char is %c\n",con);
}
else
{
fprintf(fp,"\nthe next char is a\n");
fclose(fp);

// open filehandling.txt file and check the input and output given...
}
}