C code to pass array to a function
#include<stdio.h>
#include<stdlib.h>
void func(int *arr, int n);
int main()
{
int i;
int arr[]={1,2,3,4,5};
printf("\nArray element after function : \n");
func(arr,5);//its a pass by reference only as name of the arry is the base address
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);//11 12 13 14 15
}
printf("\n");
return 0;
}
void func(int *p, int n)
{
int *arr=p;
int i;
for(i=0;i<n;i++)
{
arr[i]=p[i]+10;
}
}//end of main
Example 2:
/*
c code to pass array to a function
*/
#include<stdio.h>
#include<stdlib.h>
int* func(int *arr, int n);
int main()
{
int i;
int arr[]={1,2,3,4,5};
int *p;
p=func(arr,5);
printf("\nArray element after function : \n");
for(i=0;i<5;i++)
{
printf("%d ",p[i]);//11,12,13,14,15
}
printf("\n");
return 0;
}
int* func(int *arr, int n)
{
int i;
int *p=arr;
p=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
p[i]=arr[i]+10;//arr[i] is must not p[i]
}
return p;
}
Example 3: not a good program
/*
c code to pass array to a function
*/
#include<stdio.h>
#include<stdlib.h>
int* func(int *arr, int n);
int main()
{
int i;
int arr[]={1,2,3,4,5};
printf("\nArray element after function : \n");
func(arr,5);
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);//1 2 3 4 5
}
printf("\n");
return 0;
}
int* func(int *p, int n)
{
int *arr=p;
arr=(int *)malloc(n*sizeof(int));
int i;
for(i=0;i<n;i++)
{
arr[i]=arr[i]+10;//arr[i] is must not p[i]
}
return arr;/*if returning a pointer then function should have been called from a pointer */
}
Friday, December 13, 2013
Wednesday, December 11, 2013
Bugs in pointer commonly done in C
/* code for showing: most common error in pointer is assigning a value()not address to pointer without mallocing it. Before this note the declaration like int *ptr; tells the compiler that it will store address of an integer and not a value directly*/
#include<stdio.h>
#include<stdlib.h>
main()
{
/*case when pointer need to malloced:when ptr is directly assigned with VALUE and not address*/
int *p;//p is pointer so should store address and not value.
// *p=10;///Ptr Error: assigning value(not address) to pointer mallocing
//printf("%d\n",*p);//as a result segfault
/*so allocate memory for ptr then assign valut to pointer*/
p=malloc(1*sizeof(int));
*p=10;
printf("%d\n",*p);
/*Error not freeing memory allocated dynamically cause mem leak*/
free(p);
/*case when pointer need not to malloced: when ptr is assigned with ADDRESS and not vakue*/
int *q;//this statement tells the compiler that q will store address of an integer.
int a=10;
q=&a;//no allocation required for q when address is assigned to pointer
printf("a=%d\n\n",*q);//some times work but not always without malloc
}
Also understand well declaration of variables and pointers very well to avoid errors/bug:
int i=20;//This declaration tells three things to compiler. reserve memory for a int size, name it i and store 20 there.
int *p;//tells the compiler that p will be used to store base address of an integer.
char *p;//tells the compiler that p will be used to store base address of a string.
char *ptr="Ram"; is valid as ptr is assigned with the base address of string ie char array since name of an array gives the base address and not value.
int *p=10;//will be an array as here pointer is assigned a value and not address like above statement.
Solution in this case will be to first allocate memory then assign value.
*so allocate memory for ptr then assign valut to pointer*/
p=malloc(1*sizeof(int));
*p=10;
printf("%d\n",*p);
#include<stdio.h>
#include<stdlib.h>
main()
{
/*case when pointer need to malloced:when ptr is directly assigned with VALUE and not address*/
int *p;//p is pointer so should store address and not value.
// *p=10;///Ptr Error: assigning value(not address) to pointer mallocing
//printf("%d\n",*p);//as a result segfault
/*so allocate memory for ptr then assign valut to pointer*/
p=malloc(1*sizeof(int));
*p=10;
printf("%d\n",*p);
/*Error not freeing memory allocated dynamically cause mem leak*/
free(p);
/*case when pointer need not to malloced: when ptr is assigned with ADDRESS and not vakue*/
int *q;//this statement tells the compiler that q will store address of an integer.
int a=10;
q=&a;//no allocation required for q when address is assigned to pointer
printf("a=%d\n\n",*q);//some times work but not always without malloc
}
Also understand well declaration of variables and pointers very well to avoid errors/bug:
int i=20;//This declaration tells three things to compiler. reserve memory for a int size, name it i and store 20 there.
int *p;//tells the compiler that p will be used to store base address of an integer.
char *p;//tells the compiler that p will be used to store base address of a string.
char *ptr="Ram"; is valid as ptr is assigned with the base address of string ie char array since name of an array gives the base address and not value.
int *p=10;//will be an array as here pointer is assigned a value and not address like above statement.
Solution in this case will be to first allocate memory then assign value.
*so allocate memory for ptr then assign valut to pointer*/
p=malloc(1*sizeof(int));
*p=10;
printf("%d\n",*p);
Friday, November 15, 2013
code for a change
Concepts covered incodefora:C code for pointer variable, storage class register auto static and extern.C code for pass by value and pass by reference function, pass array to func, recursive function.C code for dynamic memory allocation malloc,calloc,realloc and free.C Code for array and string Definition, initialization,Single and Multidimensional Arrays(2-D and 3-D)
Arrays of Characters and Strings,Array of pointers, pointer to array,
C Code for structure and Union, array of structure
makefile utility program
How to work with gdb gdb
How to create your own library
socket programmin concurrent and multi client
threaded programming
IPC
perror
assert
ctags
command line parser
config file
sniffer
advance array,2-d,3-d,pass full array as function argument by value or by reference
advance string,2d array of strings and its limitations
Advance pointer concepts in C, array of pointers,pointer to array,pointer to strings, function pointer
int *ptr;/*this statement says to compiler that ptr will be used to store addres of a int. Because any pointer store address*/
char *ptr;/*ptr will be used to store strings ie array of characters*/
Thursday, November 14, 2013
codefora.blogspot.in
codefora.blogspot.in lists code/programs helpful in C linux coding.
Subscribe to:
Posts (Atom)