Tuesday, July 12, 2011

POINTERS

0 comments

A pointer in C is the address of something. It is a rare case indeed when we care what the specific address itself is, but pointers are a quite common way to get at the contents of something. The unary operator ‘&’ is used to produce the address of an object, if it has one. Thus

int a, b;
b = &a;

puts the address of a into b We can’t do much with it except print it or pass it to some other routine, because we haven’t given b the right kind of declaration. But if we declare that b is indeed a pointer to an integer, we’re in good shape:

int a, *b, c;
b = &a;
c = *b;

b contains the address of a and ‘c = *b’ means to use the value in b as an address, i.e., as a pointer. The effect is that we get back the contents of a, albeit rather indirectly. (It’s always the case that ‘*&x’ is thesame as x if x has an address.)

The most frequent use of pointers in C is for walking efficiently along arrays. In fact, in the implementation of an array, the array name represents the address of the zeroth element of the array, so you can’t use it on the left side of an expression. (You can’t change the address of something by assigning to it.) If we say

char *y;
char x[100];

y is of type pointer to character (although it doesn’t yet point anywhere). We can make y point to an element of x by either of

y = &x[0];
y = x;

Since x is the address of x[0] this is legal and consistent.
Now ‘*y’ gives x[0] More importantly,

*(y+1) gives x[1]
*(y+i) gives x[i]

and the sequence

y = &x[0];
y++;

leaves y pointing at x[1]
Let’s use pointers in a function length that computes how long a character array is. Remember that by convention all character arrays are terminated with a ‘\0’. (And if they aren’t, this program will blow up inevitably.) The old way:

length(s)
char s[ ]; 
{
int n;
for( n=0; s[n] != ′\0′; )
n++;
return(n);
}

Rewriting with pointers gives

length(s)
char *s; {
int n;
for( n=0; *s != ′\0′; s++ )
n++;
return(n);
}

You can now see why we have to say what kind of thing s points to _ if we’re to increment it with s++ we have to increment it by the right amount.The pointer version is more efficient (this is almost always true) but even more compact is

for( n=0; *s++ != ′\0′; n++ );

The ‘*s’ returns a character; the ‘++’ increments the pointer so we’ll get the next character next time around.As you can see, as we make things more efficient, we also make them less clear. But ‘*s++’ is an idiom so common that you have to know it.Going a step further, here’s our function strcopy that copies a character array s to another t.

strcopy(s,t)
char *s, *t; {
while(*t++ = *s++);
}

We have omitted the test against ‘\0’, because ‘\0’ is identically zero; you will often see the code this way.(You must have a space after the ‘=’)
For arguments to a function, and there only, the declarations

char s[ ];
char *s;

are equivalent _ a pointer to a type, or an array of unspecified size of that type, are the same thing.If this all seems mysterious, copy these forms until they become second nature. You don’t often need anything more complicated.

<<Prev Home 
Continue reading →

CREATE NEGATIVE FROM PNG IMAGE

0 comments
HI,here we will discuss about how we can create a negative image from png image by using C program.The source code of C Program is given below.Just try it.

SOURCE CODE
/*CREATE NEGATIVE FROM PNG IMAGE*/
#include <stdio.h>
#include <error.h>
#include <gd.h>

int main(int argc, char *argv[]) {
 FILE *fp = {0};
 gdImagePtr img;
 char *iname = NULL;
 char *oname = NULL;
 int color, x, y, w, h;
 int red, green, blue;

 color = x = y = w = h = 0;
 red = green = blue = 0;

 if(argc != 3)
  error(1, 0, "Usage: gdnegat input.png output.png");
 else {
  iname = argv[1];
  oname = argv[2];
 }

 if((fp = fopen(iname, "r")) == NULL)
  error(1, 0, "Error - fopen(): %s", iname);
 else
  img = gdImageCreateFromPng(fp);

 w = gdImageSX(img);
 h = gdImageSY(img);

 for(x = 0; x < w; x++) {
  for(y = 0; y < h; y++) {
   color = gdImageGetPixel(img, x, y);

   red   = 255 - gdImageRed(img, color);
   green = 255 - gdImageGreen(img, color);
   blue  = 255 - gdImageBlue(img, color);

   color = gdImageColorAllocate(img, red, green, blue);
   gdImageSetPixel(img, x, y, color);
  }
 }

 if((fp = fopen(oname, "w")) == NULL)
  error(1, 0, "Error - fopen(): %s", oname);
 else {
  gdImagePng(img, fp);
  fclose(fp);
 }

 gdImageDestroy(img);
 return 0;
}
Continue reading →
Saturday, July 9, 2011

BEEPS THE SPEAKER-C SOURCE CODE

0 comments

/*Beeps the speaker using C program*/

#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
int menu(void);

main()
{
while(1)
{
/*get selection and execute the relevant statement*/
switch(menu())
{
case 1:
{
puts("sound the speaker 1\n");
sound(2000);
sleep(2);
nosound();
break;
}
case 2:
{
puts("sound that speaker 2\n");
sound(4000);
sleep(2);
nosound();
break;
}
case 3:
{
puts("You are quitting\n");
exit(0);
break;
}
default:
{
puts("Invalid menu choice\n");
break;
}
}
}
return 0;
}
/*menu function*/
int menu(void)
{
int reply;
/*display menu options*/
puts("Enter 1 for beep 1.\n");
puts("Enter 2 for beep 2.\n");
puts("Enter 3 to quit.\n");
/*scan for user entry*/
scanf("%d", &reply);

return reply;
}
Continue reading →

EMPTY RECYCLE BIN USING C PROGRAM

0 comments

/*Source code of C program--EMPTY RECYCLE BIN*/
#include<stdio.h>

#include<windows.h>
#include<shlobj.h>
#define WIN32_LEAN_AND_MEAN



int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
if(MessageBox(NULL, "Press ok to empty the Recycle Bin.", "recycler", MB_YESNO | MB_ICONINFORMATION) != IDYES)
return FALSE;
SHEmptyRecycleBin(NULL, "", 0);
return FALSE ;
}

Continue reading →
Saturday, July 2, 2011

Small C Program-Convert IP address to it's base10 form

0 comments


/*SOURCE CODE OF SIMPLE PROGRAM--CONVERT IP ADRESS TO IT'S BASE10 FORM*/
#include<stdio.h>
#include<getopt.h>

#include<sys/types.h>
#include<locale.h>
#include<regex.h>




#define PACKAGE "ip2b10"
#define VERSION "0.0.2"
#define URL     "WWW"
#define IPEXPR  "([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})"


void print_help(int exval);
void print_version(int exval);


int main(int argc, char *argv[]) {
 regex_t re;
 int opt = 0;
 int print_flag = 0;
 long unsigned int result = 0;
 int var1 = 0, var2 = 0;
 int var3 = 0, var4 = 0;


 /* any options given ? */
 if(argc == 1)
  print_help(1);
 /* option parser */
 while((opt = getopt(argc, argv, "hvxa")) != -1) {
  switch(opt) {
   case 'h': /* print this help and exit */
    print_help(0);
   case 'v': /* print program version and exit */
    print_version(0);
   case 'x': /* output in Hex.. */
    print_flag = 1;
    break;
   case 'a': /* output some additional info... */
    print_flag = 2;
    break;
   case '?': /* no such option */
    fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt);
    print_help(1);
  }
 }


 /* enough options left ? */
 if((argc - optind) == 0)
  print_help(1);
 /* compile regular expression */
 if(regcomp(&re, IPEXPR, REG_EXTENDED) != 0) {
  fprintf(stderr, "%s: Error - compiling regular expression..\n"PACKAGE);
  return 1;
 }
 /* while parsing remaining opts.. */
 for(; optind < argc; optind++) {
  /* very basic test of ip4 dotted quad address format */
  if(regexec(&re, argv[optind], 0, NULL, 0) != 0) {
   fprintf(stderr, "%s: Error - `%s'; not a ip4 dotted quad..\n"PACKAGE, argv   [optind]);  
   continue;
  }
  /* read address */
  scanf(argv[optind], "%d.%d.%d.%d", &var1, &var2, &var3, &var4);
  /* convert address to base 10 */
  result = (var1 << 24) + (var2 << 16) + (var3 << 8) + var4;
  /* output the results */
  if(print_flag == 0)
   printf("%lu\n", result);
  else if(print_flag == 1)
   printf("%0lx\n", result);
  else {
   printf("%%\n");
   printf("ip addr : %s\n", argv[optind]);
   printf("formula : %d * (256^3) + %d * (256^2) + %d * 256 + %d\n"
     var1, var2, var3, var4);
   printf("base10  : %lu\n", result);
   printf("hex     : %0lx\n", result);
  }
 }
 return 0;
}
void print_help(int exval) {
 printf("%s,%s convert an IP address to its base10 form\n", PACKAGE, VERSION);
 printf("Usage: %s [-h] [-v] [-x] [-a] IP IP..\n\n", PACKAGE);


 printf(" -h        print this help and exit\n");
 printf(" -v        print version and exit\n");
 printf(" -x        print address in Hexadecimal\n");
 printf(" -a        print additional info\n\n");


 printf(" Please note, this is valid for IP version 4 addresses only!\n");
 exit(exval);
}
/* print version and exit with exval */
void print_version(int exval) {
 exit(exval);
}


Continue reading →
Friday, July 1, 2011

Switch Case Statement in C

0 comments
                    The switch case statement allows you to select from multiple choice based on a set of fixed value for a give expression.The value of the variable given into switch is compared to the value following each of the cases.And when one value matches the value of the variable,the computer continues executing the program from that point.


Syntax of the 'switch case' statement is:
switch(expression)
{
case value1:  /*execute code1*/
break;
case value2: /*execute code2*/
break;
.....
default: /*execute default action*/
break;
}

Example:
/*Program to convert number into word*/
#include<stdio.h>
void main()
{
int num;
printf("Enter a number:\n");
scanf("%d",&num);
switch(num)
{
case 1:
printf("ONE!\n");
break;
case 2:
printf("TWO!\n");
break;
case 3:
printf("THREE!\n");
break;
case 4:
printf("FOUR!\n");
break;
case 5:
printf("FIVE!\n");
break;
case 6:
printf("SIX!\n");
break;
case 7:
printf("SEVEN!\n");
break;
case 8:
printf("EIGHT!\n");
break;
case 9:
printf("NINE!\n");
break;
case 0:
printf("ZERO!\n");
break;
default:
printf("Invalid Number!\n");
break;
}
getch();
}
In the switch case statement,the selection is determined by the value of an expression that you specify,which is enclosed between the parentheses after the keyword switch.The data type of value which is returned by expression must be an integer value otherwise the statement will not compile.when a break statement is executed,it causes execution to continue with statement following the closing brace for switch.The default statement is the default choice of the switch statement if all cases statement are not satisfy with expression.
Continue reading →