Friday, 4 December 2015

Embedded C Programs for Interview

Embedded C Programs for Interview

1. Implement below using C program.



Given int *data= [x1,x2,x3,x4….xn];
write a function such that
data= [x1,x1*x2,x1*x2*x3,....]


#include<stdio.h>
int main()
{
int data[]={1,2,3,4,5,6,7,8,9};
int i,j=2,mul[9];
mul[0]=1;
mul[1]=2;
int size=9;
printf("\n Given array is\n");
for (i=0;i<size;i++)
printf("%d\n",data[i]);
for (i=1;i<size;i++)
{
mul[i]=data[i]*mul[i-1];
j++;
}
printf("\n Mult array\n");
for (j=0;j<size;j++)
printf("%d\n",mul[j]);
}
1,2,3,4,5,6,7,8,9
1.2,6,24,120,740


2. Using C program Implement realloc using malloc - Dynamic memory allocation.



char *my_realloc(Char *ptr, int size)
{
           if(ptr)
           free(ptr);
           char *new_ptr = malloc(ptr,size);
return new_ptr;


}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define malloc(_x) mymalloc((_x))
#define free(_x) myfree((_x))
void myfree(void * p) {
   size_t * in = p;
   if (in) { --in; free(in); }
}
void * mymalloc(size_t n) {
   size_t * result = malloc(n + sizeof(size_t));
   if (result) { *result = n; ++result; memset(result,0,n); }
   return result;
}
size_t getsize(void * p) {
   size_t * in = p;
   if (in) { --in; return *in; }
   return -1;
}


void *reallocation(void *ptr,size_t size) {
   void *newptr;
   int msize;
   msize=getsize(ptr);
   printf("msize=%d\n",msize);
   if (size <= msize) return ptr;
   newptr=malloc(size);
   memcpy(newptr,ptr,msize);
   free(ptr);
   return newptr;
}
int main() {
   char * aa = malloc(50);
   char * bb ;
   printf("aa size is %d\n",getsize(aa));
   strcpy(aa,"my cookie");
   bb = reallocation(aa,100);
   printf("bb size is %d\n",getsize(bb));
   printf("<%s>\n",bb);
   free(bb);
}


3.Write a program to initialize array using malloc, to check nth count of array.

void main()
{
int *a;
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
for (i = 0; i < o; i++) {
    scanf("%d", a + i);
}
int s=sizeof(a)/sizeof(int);
printf("\n%d\t%u\n",s,&s);
for(m=0;m<=3;m++)
printf("%d\n",a[m]);
}

4. Write a Program to print sizeof data types- sizeof(); operator.



#include <stdio.h>
int main()
{
printf("Size of data type for Char-%d!\n",sizeof(char));
printf("Size of data type for Short-%d!\n",sizeof(short));
printf("Size of data type for Int-%d!\n",sizeof(int));
printf("Size of data type for Long-%d!\n",sizeof(long));
printf("Size of data type for Double-%d!\n",sizeof(double));
return 0;
}


5.Write a program to reverse the given number.



#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,cnt=0,o,count=0,l=0,cnt1=0,j=0,k=0,m;
int *a;
char b[5];
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
for (i = 0; i < o; i++) {
    scanf("%d", a + i);
}
printf("Reversed String is\n");
while(o!=0)
{
printf("%d\n",a[o-1]);
o--;
}
}


6.Write syntax to initialize the pointer to given memory location.



#include <stdio.h>
int main()
{
   int a=9;
   int *p;
   *p=0x012;
    printf("%d\n%u\n%u\n",*p,p,&p);
   *p=a;
   printf("\n%d,%u\n",*p,&p);
   return 0;
}


7.Write a program to Reverse the string.


#include <stdio.h>


int main()
{
   int k=0,j,i=0,count=0;
   char a[]="abhishek",b[10];
   while(a[i]!='\0')
   {
       count++;
       i++;
    }
   for(j=count;j>=0;j--)
   {
       b[k++]=a[j];
   }
   for(i=0;i<=count;i++){
   printf("%c",b[i]);
  }
   printf("\n");
   return 0;
          }

Wednesday, 21 October 2015

Embedded C Programs for Interview - String

String Programs

Malloc to initialize array, to check nth count of array!!

int *a;
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
    for (i = 0; i < o; i++)    {
        scanf("%d", a + i);
    }
int s=sizeof(a)/sizeof(int);
printf("\n%d\t%u\n",s,&s);
for(m=0;m<=3;m++)
printf("%d\n",a[m]);



Program for sizeof data types


#include <stdio.h>
int main()
{
    printf("Size of data type for Char-%d!\n",sizeof(char));
    printf("Size of data type for Short-%d!\n",sizeof(short));
    printf("Size of data type for Int-%d!\n",sizeof(int));
    printf("Size of data type for Long-%d!\n",sizeof(long));
    printf("Size of data type for Double-%d!\n",sizeof(double));
    return 0;
}

Integer reverse program




#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,cnt=0,o,count=0,l=0,cnt1=0,j=0,k=0,m;
int *a;
char b[5];
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
    for (i = 0; i < o; i++)    {
        scanf("%d", a + i);
    }
printf("Reversed String is\n");
while(o!=0)
{
printf("%d\n",a[o-1]);
o--;
}
}

Pointer Initialization


#include <stdio.h>

int main()
{
    int a=9;
    int *p;
    *p=0x012;
     printf("%d\n%u\n%u\n",*p,p,&p);
    *p=a;
    printf("\n%d,%u\n",*p,&p);

    return 0;
}

String Reverse Program

Friday, 24 April 2015

bind: Address already in use , server_thread(): bind(8080) failed

bind: Address already in use

Beaglebone black , was getting this error because of my port 8080 was used for other service, I solved this the answer was just to get ride of the running service on that port else to switch to other port.


root@beaglebone:~# cd mjpg-streamer
root@beaglebone:~/mjpg-streamer# ./mjpg_streamer -i "./input_uvc.so" -o "./output_http.so -w ./www"
MJPG Streamer Version: svn rev: exported
 i: Using V4L2 device.: /dev/video0
 i: Desired Resolution: 640 x 480
 i: Frames Per Second.: -1
 i: Format............: JPEG
 i: TV-Norm...........: DEFAULT
UVCIOC_CTRL_ADD - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Mode: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Frequency: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Disable video processing: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Raw bits per pixel: Inappropriate ioctl for device (25)
 o: www-folder-path...: ./www/
 o: HTTP TCP port.....: 8080
 o: username:password.: disabled
 o: commands..........: enabled
bind: Address already in use
bind: Address already in use
 o: server_thread(): bind(8080) failed



Step 1 :


 netstat -p
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.40.25:59952     ssl.discourse.org:https ESTABLISHED 2922/chromium-brows
tcp        0      0 192.168.40.25:47608     nrt13s18-in-f24.1:https ESTABLISHED 2922/chromium-brows


Step 2 :
fuser 8080/tcp 

Step 3 :
fuser -k 8080/tcp