Showing posts with label Basic C Concepts. Show all posts
Showing posts with label Basic C Concepts. Show all posts

February 25, 2016

Bit Fields in C

Bit fields can be used in a structure or union. They are used where memory is very scarce. They can be used when the highest value that will be represented by a variable is very small.For instance, leat a variable int a is used to store 4 states say, (0, 1, 2, 3). The number of bits required to represent the highest value '3' is 2. If an integer is used to save these states, 32 bits are allocates in which 30 bits are unused. If 'n' such variables are used, the number of bits wasted will be 30*n. By using bit fields represented as int a:2; Though complete 32 bits are allocated for the int type, a represents only the first two LSBs. Any further bitfields declared as in below example will be packed within this remaining 30 bits until the total size reaches above 32 bits (4 bytes).

#include <stdio.h>
{
int a:5;
int b:10;
int c: 12;
int d;
} n1;

int main()
{
n1.d = 0x12345678;
printf("a 0x%x b 0x%x c 0x%x d 0x%x\n", n1.a, n1.b, n1.c, n1.d);
}

Output:
a 0xffff fff8 b 0xffff fe78 c 0x678 d 0x12345678

February 15, 2016

Double pointer usage and dynamic memory allocation using box diagram

Consider the below example,

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int **p;     /* A pointer to a pointer to an integer */
    int a;

    p = malloc(sizeof(int *));

    *p = malloc(sizeof(int)); /* OR *p = &a; */

    **p = 10;

    printf("a = %d  **p = %d\n", a, **p);
} 

Output:
a = 10 **p = 10

In the above example, 'p' is a double pointer which means, 'p' will point to another pointer which in-turn is pointing to an integer variable.

Now, we have 'p' which is a pointer to a pointer. 'p' has its memory allocated at some address location say, '1000'. Lets say 'p' is ponting to another pointer 'x' which is at the address '1004'. Then 'x' in-turn will be pointing to variable 'a' located at say, 1008.
Box Diagram representation of pointers

'p' points to 'x'.
'x' points to 'a'
p->x->a or (p->*p->**p)

if say, pointer 'x' is not declared, then the second link in the above representation will break.

p->( )->a or (p->( )->**p)

It means, there is no memory representing link-2 which will have the address of 'a'. Hence, by using dynamic memory allocation, we can first allocate a memory by declaring a pointer variable 'x' and make the link.

Or, dynamically allocate a memory of type (int *) and make 'p' to directly point to that memory and put the address of 'a' in that allocated memory. The above program represents the second case.

In other words,

if you have a double pointer say,
int **p;

It means, at the declaration, memory is allocated for the variable 'p' which can point to another integer pointer which in turn will point to an integer variable. i.e., if it is a double pointer, then there needs three memory locations, one for 'p' of type 'int **' (pointer to a integer pointer) which is allocated during declaration, another for '*p' of type 'int *' (pointer to an integer) and the third memory for integer to which it points to (say, an integer value '10'). So, two memory allocations are needed.
p = malloc(sizeof(int *));

*p = malloc(sizeof(int));

The first statement will allocate the second memory for *p (type: int *) and its address is stored in p (type: int **).

The second statement will allocate the third memory required to store an integer '10' (type: int) and its address is stored in *p (type: int *).

The third memory can also be  a integer variable (say, count) in which case, the second statement above can be modified as
*p = &count;

Similarly,

For triple pointer, ***p, there shall be 4 memory locations, 3 for pointers and one for the variable it is pointing to and so on..

But, why do we need a double or a tripple pointer?

Pointer usage in C

C provides pointers which are used to store the address location of another variable of a particular data type.

Declaring a pointer to an integer variable,

int *p;

where, 'p' is a integer pointer that can point to (store the address of) a variable of type 'int'.
i.e., an integer pointer should be used to store the address of an integer variable only.

Similarly, a character pointer shall point to a character variable only. Failing to follow this, may result in an undefined behaviour.

Now, lets consider an example:

#include <stdio.h>

int main(void)
{
    int a = 10; /* An integer variable */
    int *p; /* A pointer to an integer */

    p = &a;

    printf("a = %d  *p = %d\n", a, *p);
} 

Output:
a = 10 *p = 10

In the above program, integer 'a' will be allocated a memory in stack say '0x1000' and the value '10' is stored in that memory location. Integer pointer 'p' will be allocated a memory in stack say '1004'.

Now, '&a' represents the address allocated to store the value in 'a' i.e., &a = 1000.

Now, this value 1000 is copied to 'p'. Now, the variable 'p' contains 1000.

Then, in the print statement, '*p' means de-referencing 'p'.

i.e., contentof(p) or contentof(1000) = 10. Hence, the output 10.

With this understanding, lets look at how to use double pointers and dynamically allocate a memory in the next example.

December 11, 2015

C Code | Tic Tac Toe game

Given below the code for tic tac toe game with variable grid size. Know about the game at Wikipedia or play online at http://playtictactoe.org/ or get the android app at google play.

C Code | Hangman game

In Hangman game, you need to find random words before the hangman image is completed. For every wrong answer, image will grow by one step. Code for hangman is given below. To know more details about the game, read at Wikipedia and play at hangman.no

C Code | Master mind or Bulls and Cows game

Giving the code below, for details about the game, read at Wikipedia or play at archimedes-lab.

In the below code, a 4 digit number with unique digits is generated by swapping an array having 10 elements (0 to 9) for N_SWAP number of times. The player is given 8 chances to identify the 4 digit number. For every input, the game will show number of bulls and cows.Digits at the right place are counted as bulls and the digits at the wrong place are counted as cows.

Hint: If bulls + cows = 4, it means the number you entered have the right digits but positionally misplaced.

Example: Let the secret number be 1234. Guessed number be 1203. Now bulls = 2, cows = 1.

C Code | Arithmetic quiz game

Below this is the code for arithmetic quiz game. For understanding the game, play at thatquiz.org

October 15, 2015

C Code | Guess a number game

Below is the C code to build "Guess the number" game. For details on how does guess the number game works, visit GuessMyNumberGame or Wiki page.

Receive all updates via Facebook. Just Click the Like Button Below

▼

You can also receive Free Email Updates:

dgpride - Study Zone - Free Books - Tamil Lyrics

Copyright © 2008 -2012 dgpride. All rights reserved.

Subject/Topics

2 Marks (26) 8051 (1) AC Machines (7) Animations (1) Anna University Chennai (31) Arduino (4) ARM (3) Audio (1) Basic C Concepts (8) Basic Electronics (13) Basic principles (9) Book list (1) CAD (1) Chemical (2) Circuit theory (6) Civil (2) Cloud Computing (1) Communication (4) Competitive exams (2) Computer Architecture (4) Control system and components (9) CSE (40) Curriculum (4) DC Machines (9) Did you know (14) Digital (13) DLC (4) Documentation (1) DSP (1) EC 2201 (3) ECE (45) EDC (1) EEE (34) EIE (63) Electrical (35) Electronics (43) Electroplating (2) Emacs (1) Embedded basics (19) Embedded C Programming (19) Embedded Linux (5) Embedded System (22) Engineering basics (15) Environmental Science (1) Fibre Optics (1) Filters (2) FPGA (1) GATE (3) General (7) GNU (4) Handwritten (1) Hobbyist (15) How to (8) HTML (3) Humanities (2) IC Engines (7) ICE (5) Industrial Electronics (10) Industrial Instrumentatin (2) industrial process (2) Instrumentation (21) IoT (2) IT (2) Laboratory Manuals (17) LabVIEW (2) Lesson notes (2) LIC (2) Links (9) Linux (8) Magnetics (1) Management (1) Mechanical (5) Mechatronics (9) Microcontrollers (14) Microprocessors (9) Microsoft (1) Motivation (1) Must Know (11) Networks (1) NuttX (1) Objective type (1) Open Source (1) Opportunities (7) Oscillators (2) Part Time (1) Physics (1) Post Graduation (1) Power Electronics (12) Power Plant Engineering (2) Power Supplies (2) Previous GATE Papers (1) Process Control (2) Project (4) Protocol (1) R2008 (11) R2009 (1) R2013 (1) Recruitment (2) Research (2) Robotics (9) RTOS (3) Signal Processing (8) Signals and Systems (4) SMPS (1) Software tutorial (4) Stepper Motor (2) Syllabus (5) Texas Instruments (2) Thermodynamics (2) Training and Placement (6) Transducer Engineering (2) Transformer (2) Transmission (1) Tutorials (48) Two Marks (26) U-Boot (1) University Question Papers (16) Verilog (1) Video (4) Virtual Instrumentation (3) Visual Basic (21) VLSI (11) Web designing (4) Wi-Fi (3) Wireless (6)