Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Find address of char, string, integer



#include<stdio.h>
#include<conio.h>
 main()
  {
   char *chp,*sp;
   int i;
   char ch,s[10];
   int *ip;
   clrscr();
   printf("Enter a char:");
   scanf("%c",ch);
   printf("Enter a string:");
   scanf("%s",s);
   printf("Enter a integer:");
   scanf("%d",&i);
   chp=&ch;
   sp=s;
   ip=&i;
   printf("\nchar\tadd\tstring\t\tstringadd\tint\tint add\n");
   printf("%c\t%u\t%s\t\t%u\t\t%d\t%u",ch,&chp,s,&s,i,&i);
   printf("\nchar pointer value is:%u",chp);
   printf("\nstring pointer value is:%u",sp);
   printf("\nint pointer value is:%u",ip);
   getch();
  }

Qserch , string, dynamic pointer array

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void sortstrarr(void *array, unsigned n);
static int cmpr(const void *a, const void *b);

int main (void)
{
char **strarray = NULL;
int i = 0, strcount = 0;
char line[1024];

while((fgets(line, 1024, stdin)) != NULL)
{
if(strlen(line) == 1)
continue;

strarray = (char **)realloc(strarray, (strcount + 1) * sizeof(char *));
strarray[strcount++] = strdup(line);
}

printf("### Before ###\n");
for(i = 0; i < strcount; i++)
printf("%2d: %s", i, strarray[i]);

sortstrarr(strarray, strcount);

printf("### After ###\n");
for(i = 0; i < strcount; i++)
printf("%2d: %s", i, strarray[i]);

/* free mem... */
for(i = 0; i < strcount; i++)
free(strarray[i]);

free(strarray);
return 0;
}

static int cmpr(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}

void sortstrarr(void *array, unsigned n)
{
qsort(array, n, sizeof(char *), cmpr);
}

Labels