Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

please explain the below c program

Status
Not open for further replies.
Pointers are very easy.

You define a ordinary variable this way

var_type var_name

You define Pointers this way

var_type *var_name


Eg: a pointer to int type is declared as


int *ptrCount


If count is a int type variable then you can get or set the value of Count indirectly by using pointer (like *ptrCount)

To do this you have to assign the address of Count to ptrCount and you do this like

ptrCount = &Count;

& before Count is the addressOf operator and it gets the address of the variable Count and assignes it to the pointer (pointer variable) ptrCount

Both Count and ptrCount have right-value and left-value.

Right value is the value the variable contains

Left value is the address of the variables in memory.

A pointers right value is address of the variable to which it is goint to point to.

If value od Count is 10 and its address is 0x1234 and address of ptrCount is 0x4321 then by doing


ptrCount = &Count;

you are setting the value of ptrCount to 0x1234


If you want to get or set the value of Count using ptrCount then you have to use dereferencing or indirection operator *


If you have another int type variable named result and want to assign the value of Count to it then you do like this


result = *ptrCount;

It means assign to result the value (right value of Count) the pointer ptrCount is pointing to.

ptrCount is pointing to Count and Count contains 10. So, 10 is assigned to result.

To set value of Count indirectly you do like this...

If you want to increase the value of Count to 20 then...


a) Count = *ptrCount + 10;

b) or Count = *ptrCount * 10;

a means add 10 to the value to which ptrCount is pointing to and assign it to Count.

b means multiply the value to which the pointer ptrCount is pointing to by 2 and assign the new value to Count.
 
Yeah It was not hard as you think, first you have to learn a simple pointer after that you have to access a array using pointer then you will be very clear....


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
 
main()
 
{
 
int a, b, *c, *d, e;
 
a = 10;
b = 5;
 
c = &a; // address of a
 
d = &b; // address of b
 
e = (*c) * (*d); // multiply the data of c and d
 
 
printf("Result is : %d\n" , e);
 
}



look at the above program there is only two operators you wanted to know they are * and &
& => denotes address of a variable
* => denotes content of the address
 
hi brothers, can u provide me the links for learning data structures in c ,especially some of the topics like stacks,queue,linked list,tree ,graphs .thanking you in advance..
 

brothers ,i am having clarifications regarding some concepts in data structures in C
.can u clarify that??
 

there are many algorithms in data structures.linked list,doubly linked list,circular linked list ,hash table,trees,graphs,queues,stacks and some of the sorting algorthms.
1) how these all finds applications in real life??
2) whats meant by best case scenario and worse case scenario explained in some algorithms?
3) is there any relation with data structures and linux application programming ??
 

Ans 1: Data structures are used in database management which is quite a real world application and required too. The searching and sorting algorithms for data structures are quicker and efficient.

Ans 2: Best/Worst case scenario is mostly used in sorting/searching algorithms (at least till what I know related to data structures). The best case scenario will be the case when the data which we are sorting is already sorted or the data we are finding is the first to appear.
The worst case is the opposite of this i.e. the data we are sorting is in the reverse order of the sorting we require hence making the program do more comparisons etc.

Ans 3: Data structures (specifically) is not used in Linux kernel. All the kernel data is stored using this concept only , because the linked list concept is very useful when we want to avoid the use of contiguous memory. All the kernel process data, inode etc are stored using the linked lists.,
 
Data structures is just the way to arrange the data.. among these, each has its own application while programming.. Not all data structure will be used in programming. Worst case scenario's means the probability of maximum comparison we have to do in sorting or searching algorithms.
 
You know we are reading it because it is necessary in real life, in every c programming we are using any one of this data types and any one of the process unknowingly even out controller itself uses stacks and queues.... In simple you have to learn them with each of them advantage and disadvantage, then only you can use them when you need.. for example double linked list over single linked list gives double side access, If you writing a program and at some point you need dynamic allocation means you will go for linked list if you need double end access means you will go for a double linked list....

best case scenario means If you writing a algorithm for shorting it can give the result easily if the data is already in ascending order then that is the input in best case worst case means Its opposite..
 
Hi rangerskm,

1) Data structure are mainly the way to arrange the data such that it will be very easily used in your code ( I mean without much of handling complications) for an example you want to store values of currency every day once with time which is single variable so you can use single dimension array.....now same thing you want to store multiple times in a day where time is also variable then you can use eighter two arrays or one 2D array or a structure in C there are way the data structure works.....

2) Now how there data structure are arranged is what you mentioned are the methodologies by which the data is stored like link list or circular list is a collection of data structures ..... based on the application need you need to chose what methodology that you want to use..... there methodologies are well thought through such that one can do six basic operations like add, search, sort, edit, delete and save. So the data structure algorithms tell us how to use the methodology with the chosen data structure in most efficient way. The worst case and best case scenarios are the measure for the efficiency of the algorithm based on most of the time Order of computation called as big O notation.

3) Now whole computer world revolves around the data, it's arrangement, it's processing and it's use for application to generate some results so.....these concepts really help in all the environments let it be linux or windows or any thing for better efficient application or design.......

Good Luck
 
please explain this piece of code which is used in tree
Code:
void destroy_tree(struct node *leaf)
{
  if( leaf != 0 )
  {
      destroy_tree(leaf->left);
      destroy_tree(leaf->right);
      free( leaf );
  }
}
 

please explain this piece of code which is used in tree
Code:
void destroy_tree(struct node *leaf)
{
  if( leaf != 0 )
  {
      destroy_tree(leaf->left);
      destroy_tree(leaf->right);
      free( leaf );
  }
}

leaf is a pointer of a node in tree if it is called and if its a valid pointer then their all children will be free(memory deallocated) recursively...
 

As I understand the code it is the recursive function which uses the leaf node ( pointer well said by Venkadesh_M ) as input variable and it deletes all the children of the tree from that leaf node.... As per my understanding it dose not completely destroy the tree but the section of tree based on what is the node is selected to be made as leaf node ..... By definition of leaf node in binary - A leaf node has no children..... if leaf node is root node then only it destroy complete tree....

Good Luck
 
Tree is a data structure where every node (starting form the root) has leaves(right and left leaf or even both). Trees have leaves that means that every node has pointers to maximum 2 other nodes (I'm talking about a binary tree).
So if we want to destroy a tree, we cannot just start from the root and start freeing the memory, coz it will contain the pointers to the subsequent leaf.
Hence this recursion technique is used, which will first traverse to the end-most node and then start freeing from there on wards.
 
@ 'errakeshpillai' - I am not sure how you said that, as in the code the root node is passed as an input parameter to the function it will get freed at the last recursion of the operation.... I mean after freeing all the right and left node of the tree.... but you can correct me If I am wrong.....

with regards,

Milind
 

void destroy_tree(struct node *leaf)
{
if( leaf != 0 )
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
free( leaf );
}
}


@milind.a.kulkarni: Here the first instruction in the function is a call to the same function (itself) by passing the pointer to the left node. So this will carry on till it reaches the leftmost leaf. Then while continuously returning from the function calls it will go to the rightmost leaf and free those.
So this recursion will free from the bottom of the tree.
 
Yes.... you are right .... that mean if I pass root node as input parameter it will destroy the complete tree ..... not otherwise..... but in your post you said .... we will not start freeing the memory from root node.... eventually to destroy the tree if the root node is input parameter for the function .... of course in recursion it is always the end point get operated first then the it circulate up words.... I think that what your point was...... I think I miss read what you want to say is input parameter of function (leaf node) is root node will not destroy the tree..... sorry for that
 

hi rakesh ,

as you said the pointer reaches bottom of the nodes.which one node will be deleted first ,left or right??how it happening
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top