Showing posts with label c++. Show all posts
Showing posts with label c++. Show all posts

Friday, June 24, 2011

No More Confusion with Pointers: A short technical paper for understanding Pointers


Pointers, the concept which a c beginner find difficult to understand. Even if you once read it carefully, you cannot be hell sure that you will be able to work with pointers conveniently. Here is a quick technical article which may help you understand pointers deeply.

Misconception: Pointer is an address to a VARIABLE.

A pointer points to an address, but the address need not be the address of a variable. It may be address of any location in the computer memory. For example,

int i=10, *pi;
pi=&i;
 
Here ‘pi’ is a pointer variable pointing to the location of ‘i’. Be careful, it does not point to ‘i’, it points to its location only. Now if we change either ‘i’ or ‘*pi’, the corresponding value at the location will change. Here 
arises the misconception that ‘pi’ is associated with ‘i’, which is not. Consider the following example:
int *pi;
pi=(int *)malloc(sizeof((int));
/* malloc is a function to dynamically allocate memory to a pointer. It returns ‘void *’, which is typecasted as ‘int *’ */
*pi=1024;
printf(“The value at pi is”,*pi);
free(pi);

Here ‘pi’ is a pointer which points to an integer value. But it does not points to some variable, but a memory location is allocated to it using ‘malloc’.
Hence we conclude that a pointer is a variable which points to a memory location and not particularly the location of a variable.

Wednesday, June 22, 2011

C program to evaluate a polynomial using HORNER’s Rule

Recently I was going through Data Structures in C, and I came to the problem of evaluating a polynomial using Horner’s rule. Horner’s rule is a strategy for evaluating a polynomial A(x) at a point using a minimum number of multiplication.

The following C program is a correct one, but the thing I want to emphasize is trouble with the use of global variables. Lets go through the C code first:
#include<stdio.h>
#include<conio.h>
int horner(int,int);
int count=0;
void main()
{
     /*Horner's rule for evaluating a polynomial */
     /* let us take a0=0,a1=1,a2=2.. and so on */
    
     int n,x,h=0; //n is order, x is value of X in polynomial.
     scanf("%d %d",&n,&x);
     h=horner(n,x);
     printf("%d",h);
     getch();
}

int horner(int n, int x)
{
    int i;
    if(count!=n)
    {
                i=count;
                count++;
                printf("%d+%d*(",i,x);
                return (i + x*horner(n,x));
    }
    else
    {
        printf("%d))=",count);
    return count;
   
}
    
}
Initially I faced a big problem with the above program when I was not using the local variable ‘i’ in the ‘horner’ function. You can check the output with and without the use of ‘i’. The problem was that all the return statements were being evaluated after the last ‘horner’ got evaluated for the recursive function, and in the meanwhile, the global variable ‘count’ got changed every time ‘horner’ was evaluated. Since the return statement depends upon ‘count’, the output came out to be deviated from the expected value depending upon the size of input. Hence the variable ‘i’ came into existence.

Sunday, June 19, 2011

Six Free C\C++ ebooks by Microsoft


C \ C++ logo
This week Microsoft announced that it's working on making C++ better suited for massive parallelism. This follows Google's recent benchmarks that showed that C++ is still faster than most programming languages (at least if you know how to optimize it correctly). Also, Google has been working on incorporating C and C++ support into Chrome.
In other words, C++ is a strong and important language, and will continue to be even in a browser dominated landscape. Here are a few resources for leaning C++, and its predecessor C.
Introduction to C Programming
You don't need to know C to learn C++, but many C++ books assume knowledge of C. If you're completely new to programming and want to learn C++ from a free book, please see How to Think Like a Computer Scientist below.
However, if you are new to programming and want to start with C, you might want to start with Introduction to C Programming by Rob Miles. A PDF version can be found here.
Miles has also written free e-books on C#.

The C Book

The C Book
The C Book by Mike Banahan, Declan Brady and Mark Doran is an introduction to C for experienced programmers. The print edition, first published in 1991, is no longer in print.

How to Think Like a Computer Scientist C++ Version

How to Think Like a Computer Scientist C++ Version is the C++ 'port' of Allen B. Downey's classic introduction to programming. The Python version has been used by MIT for its introduction to programming. It assumes no prior programming experience.
There are also versions in Python and Java, and a Ruby version is in progress.

Thinking in C++

Thinking in C++ cover
Thinking in C++ by Bruce Eckel aims to 'move you, a little at a time, from understanding C to the point where the C++ mindset becomes your native tongue.' It's written with the expectation that the learner have existing knowledge of C syntax. It begins by introducing object oriented programming and moves into covering more advanced C++ over the course of two volumes.
A print version is also available.
Eckel also wrote Thinking in Java which we included in our round-up of free Java e-books.

C++ Annotations

cpp_annotations_0611.jpg
C++ Annotations is a free e-book by Frank B. Brokken of the University of Groningen. He uses it as the primary text of his course on C++, and it's written for programmers already familiar with C.

Visual C++ 6 Unleashed

Unleashing Visual C++
Visual C++ 6 Unleashed by Mickey Williams and David Bennett covers programming in C++ using Microsoft's IDE. A print version is available as well.

Want Even More?

There are many more free e-books on C and C++, including many on more advanced topics. You can find more here, here and here.
If you want to learn C# or Objective C, we have separate round-ups for you here and here.