Skip to main content

Posts

Showing posts from October, 2017

char* and char[] : Char Array vs Char Pointer

In C/C++,  Char[] is basically an array of characters. So, if we have something like below :- char c[] = "hello" ; It means, we have an array "c" which has been initialized to string "hello". It is equivalent to following char c[] = { 'h' , 'e' , 'l' , 'l' , 'o' , '\0' }; It essentially means, that "c" is an array of length 6 (based on the length of string literal). In memory, array is stored in continous memory cells which can be accessed by name "c". A copy of this array is present in stack and just like any other arrays, you can modify its contents, i.e. c[0] = 'H' ; // Perfectly legal Note, that you can not assign a new string literal to "c" without re-declaring it but can change the individual character by array index. Char* is a pointer to string literal. In this case "c" is a "pointer to char" and initializes it to

Structures and Typedef in C

I'll use this post to talk about creation & initialization of structures and also about the importance of using typedef while working with structures in C language. Structures are basically a data type which can be used to store group of objects of same or different data types as opposed to arrays which can store group of same data type only. # General Syntax struct tag_name { type member1; type member2; }; In the above case, struct is the keyword that is used to define structures, tag_name is an optional argument. Lets take an example to understand this better # Create a type "struct Point", which we can use later to declare variables of this type struct Point { int x; int y; }; # Create two variables pt1 and pt2 of type "struct Point" struct Point pt1, pt2; #Creation of type and variable declaration can be merged as well. It will basically create a type of struct Point and create two variables of this type struct

Pointers and Arrays in C

While learning C language, one of the things that I noticed being mentioned in tutorials were using "Arrays and Pointers and interchanegbly". In this post, I'll try to explain what I learnt :- To start with Arrays and Pointers are NOT same. Arrays are arrays and pointers are pointers. Depending upon the context (mostly), array names are implicitily converted to pointers.  When an array is  If we have an expression like // Declare an array of integer type which can hold 10 values and an integer pointer int a[10]; int *ap; When an array is used an an value, its name represents the address of the first element. When an array  is not used as an value, its name represents the whole array. // Here array "a" holds the address of the first element. // a[2] is actually referenced by address of first element + offset (index of array) (i.e. pointer arithemetic is used) // When an array is de-refernced [] then x[y] can be considered as pointer arithemet

GDB in Eclipse CDT: Failed to execute MI command

Using Eclipse CDT on Mac may give different errors while debugging like below : GDB in Eclipse CDT: Failed to execute MI command / program terminated with signal SIGTRAP, Trace/breakpoint trap To solve this problem requires :- - Installation of latest gdb using Brew - Create a new certificate for gdb and then codesign it Open Key Chain Access Create Certificate Assistant\Create new certificate Select name as "gdb-cert" and set identity type to "Self Signed" Set certificate type to "Code Signing" and select "Let me override defaults" Click on continue till you get "Specify a location" and choose "System" - Codesign the gdb sudo codesign -s gdb-cert /usr/local/bin/gdb   - Create a new .gdbinit file and edit the Eclipse preferences with the full path to gdb and .gdbinit echo "set startup-with-shell off" >> ~/.gdbinit

C pointers with pre-increment and post-increment operators

Pointers with pre-increment and post-increment operators is one of the confusing topics for the new learners. In this post, I'll try to explain the concept with an example. I'll use the below code example to explain the concept. #include<stdio.h> #include<stdlib.h> int main() { int x; int *xp; xp = &x; x = 5; printf("Original Value of variable x is %d\n",x); printf("Value of x with post-increment operator is %d\n",x++); printf("Value of x after post-increment operator is %d\n",x); x = 5; printf("Original Value of variable x is %d\n",x); printf("Value of x with pre-increment operator is %d\n",++x); printf("Value of x after pre-increment operator is %d\n",x); x = 5; printf("\nOriginal Value of variable x is %d\n",x); printf("Value of x with post-increment operator called on pointer is %d\n",*xp++); printf("Value of x after post-increment operator calle