C Programming and Computer Geeks: C Tutorials

Search MY Blog

Showing posts with label C Tutorials. Show all posts
Showing posts with label C Tutorials. Show all posts

Tuesday 15 April 2014

Pointer to array and Array of Pointers



Pointers and Arrays
Pointers and Arrays
What Is the difference between the following three declarations?

int *ptr1[5];
int  (*ptr2)[5];
int* (ptr3[5]);



Answer:
int *ptr1[5];
   Here in int *ptr1[5], ptr1 is an array of 5 integer pointers (An array of int pointers).

int  (*ptr2)[5];
   And in int (*ptr2)[5], ptr2 is a pointer to an array of 5 integers (A pointer to an array of integers).

int* (ptr3[5]);
   This is same as ptr1 (An array of int pointers).


What will be the Output of the below program?

void main(void)
{
     int arr1[4] = {1, 3, 5, 7};
     int arr2[4] = {2, 4, 6, 8};

     int (*ptr[2])[4] = {arr1, &arr2};
    
     printf("%d, %d", (*ptr[1])[2], *(**(ptr) + 3));

}

Try to explain (*ptr[1])[2] And *(**(ptr) + 3) expressions and outputs by step by step..
Learning Points: Pointers to array/Array of pointers and resolving pointer expressions.

Program Explanation:
int (*ptr[2])[4] = {arr1, &arr2};
Here ptr is a array of 2 pointers,In which each pointing to an array of 4 integers.
from the above C statement this ptr is assigned as {arr1,&arr2}.
Here arr1 is a base address to array arr1 also called pointer to the first element of array.
And &arr2 is an address of array(arr2) also called entire array address).
ptr array elements(ptr[0],ptr[1]) expects the address of type int(*)[4]. But ptr[0] is assigned arr1,which is of type int*.
So this is the flaw in code. This may not lead to any compilation error, but it gives warning.
Even though you have given base address(arr1) to ptr[0], it will considers it as pointer to an array of 4 int (array address or entire array address).

printf("%d, %d", (*ptr[1])[2], *(**(ptr) + 3));
From the above C Statement

( *ptr[1] )[2]:
( *ptr[1] )[2] ==> (* ( ptr[1] ) ) [2]  ==> (* ( &arr2 ) ) [2] ==> ( arr2 ) [2] ==> arr2[2] ==> which equals the value 6

*(**(ptr) + 3):
**(ptr) ==> *(*ptr) ==> *(&arr1) ==>arr1(base address) 
Now

*(**(ptr) + 3) ==> *( arr1 + 3) ==> Nothing but arr1[3] ==> which equal to the value 7

Output:
6, 7

Please Correct me if anything is wrong. Please share your inputs and suggestions in comments.
Find more on C Pointer Concepts Here


Tuesday 24 December 2013

C Language Advantages



Advantages
Ø  C language is a base or building block for many other currently known languages.

Ø  C language has varieties of data types and powerful operators. Due to this, programs written in C language are efficient, fast and easy to understand.


Ø  C is highly portable language. C programs written for one computer can easily run on another computer without any change or by doing a little change.

Ø  There are only 32 keywords in ANSI C and its strength lies in its built in functions. Several standard functions are available for developing programs.


Ø  Another important advantage of C language is its ability to extend itself.
A C program is basically a collection of functions that are supported by the C library this makes us easier to add our own functions to C library. Due to the availability of large number of functions, the programming task becomes simple.

Ø  C Programming is a structured programming language. This makes developers to think of a problem in terms of function modules or blocks. The Collection of these modules makes a complete program or software. This modular structure makes program debugging, testing and maintenance more easier.

C Language Advantages over C++
C vs C++


Disadvantages

Ø  There is no runtime checking in C language.
Ø  There is no strict type checking. For example, we can pass an integer value for the floating or character data type.
Ø  C does not have concept of OOPs, that’s why C++ is developed.
Ø  C does not have the concept of namespace.

Ø  C does not have the concept of constructor or destructor.

Monday 23 December 2013

What is "void" type in C/C++ ?


Before going to the void data type, First let us understand the data type concept.
In C/C++ programming language, the data types refer to an extensive system used for declaring/defining variables/objects or functions of different types. The type of a variable determines how much memory space it occupies in storage and how the bit pattern stored is interpreted.
char, int, float, double, void etc.. are some of the data types in C language.
Data types in C/C++
Data Types in C/C++

Now coming to the void data type..
void is basically a keyword to use as a placeholder where you would put a data type, to represent "no data".
void actually refers to an object that does not have a value of any type.

When "void" type is used in function definition it means that function will not return any value.

When "void" is used with pointer type variable declaration eg: void* a or void* b etc. It means any empty type i.e. later in the program you can point any data type(int or float type variable) to a or b respectively.


Click here for more C Tutorials and Concepts.

Saturday 21 December 2013

Difference between Compiler and Interpreter

The main difference between Compiler and Interpreter is:
Compiler translates the program statements(Source Code) into Machine Code in one go i.e. all lines of program simultaneously. That's why all compiler based languages shows all the errors together.

While interpreter translates the source code into machine code one statement at a time. That's why program execution cannot proceed to next statement until the previous error is corrected/rectified.

Find more C Concepts here..

Thursday 19 December 2013

String to Integer conversion function


int strtoint(char str[])
{
  int i=0,num=0;
    
  while (str[i]!='\0')
  {
     num=num*10+str[i]-'0';
     i++;
  }

  printf("Entered String = \"%s\" , After String to Int = %d",str, num);

  return num;
}

Output:

String to Integer Conversion
string to integer

 
Click here for more C Faq

Search This Blog