Sign In

Related To Software

vaishali Khandave vaishali.khandave@yahoo.com India

Hardware: 8051 Microcontroller,gsm,rfid Software: Embedded C,keil V5,c53 Compiler

A separate function is called to copy the elements in a buffer of array..the values are assigned and printed in this function...
But in main function the values are not printed?Why?
I am returing the address of the array ie array name
plz need help

Images

Program

#include <reg51.h>
#include <delay.h>
#include <lcd8bit.h>
#define nbytes 12


unsigned char* send()
{
	unsigned char i;
	unsigned char buf[nbytes];
	unsigned char *buff=&buf;
	
for(i=0;i<nbytes;i++)
	{
		buf[i]='A'+i;
	}
	buf[i]='';
	
	lcd_cmd(0X01);
	lcd_cmd(0X80);
	lcd_string("buf ");
	lcd_cmd(0xc0);
	lcd_string(buf);
	delay_ms(1000);
	return buf;
}

main()
{
	unsigned char i;
	unsigned char buf1[12];
  unsigned char *buff1=&buf1;
	lcd_init();

	buf1[12]=send();
	for(i=0;i<12;i++)
	{
	lcd_cmd(0X01);
	lcd_cmd(0X80);
	lcd_string("buf1[");
	lcd_integer(i);
  lcd_cmd("]=");		
	lcd_cmd(0xc0);
	lcd_data(buf1[i]);
	delay_ms(1000);
	}
	
	while(1);
}


Replies

R Avinash Gupta
2017-01-04 07:33:44
Very basic problem.

The variable unsigned char buf[nbytes] on line 10 is local to the function send() which is lost as soon as the function returns.

R Rik Mitra
2017-01-08 13:18:45
Declare the char buf[12] as global variable.

Post a reply to vaishali Khandave

Think you can help vaishali Khandave ? Then post your thoughts that might help vaishali Khandave. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.