• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to pass string as argument in function

Status
Not open for further replies.

Parth86

Member
Hi

How to pass string as argument in function . I understand array of strings program in c example
C:
#include<stdio.h>
int main (void)
{
    unsigned int i=0;
    unsigned char array[15]= "Hello Forum";
 
    while(array[i] !=  '\0')
     {
         printf("print array element : %c \n", array[i]);
         
         i++;             
 
     }
  
   return 0;
   
}
This is my attempt to solve problem
C:
#include<stdio.h>

void message (unsigned char string [])
{
   printf("print string : %s \n", string);
}

int main (void)
{
   
   return 0;
   
}

How to pass n string's as argument in function?
 

Pommie

Well-Known Member
Most Helpful Member
Whenever you post a question at least tell us what processor/compiler etc.

This is how I do it on a pic where the message is in ROM.
Code:
void PutMessage(rom char *Message){
char i=0;
    while(Message[i]!=0)
        PutChar(Message[i++]);
}
to call it you do PutMessage("Hello World");

Mike.
 

Parth86

Member
Whenever you post a question at least tell us what processor/compiler etc.

This is how I do it on a pic where the message is in ROM.
I am really sorry. I forgot to mention compiler name. I have installed mingw gcc compiler on my windows laptop
if I run your program then compiler show some errors
C:
#include<stdio.h>

void PutMessage(char *Message)
{
   char i=0;
   
    while(Message[i]!=0)
        PutChar(Message[i++]);
}

int main (void)
{
   PutMessage("Hello World");
   
   return 0;
   
}[code]  
Compiler error report
hello.c:8:9: warning: implicit declaration of function 'PutChar' [-Wimplicit-function-declaration]
         PutChar(Message[i++]);
         ^~~~~~~
C:\Users\MREMBE~1\AppData\Local\Temp\cceQHj1A.o:hello.c:(.text+0x2a): undefined reference to `PutChar'
collect2.exe: error: ld returned 1 exit status
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Just use :- printf( "%s", string); to print the whole message.
Or putch( string[x]); to print individually..

In your code above... There is no function called PutChar... The correct function is putch();
 

Parth86

Member
Just use :- printf( "%s", string); to print the whole message.
Or putch( string[x]); to print individually..

In your code above... There is no function called PutChar... The correct function is putch();
Still not getting correct way
C:
#include<stdio.h>

void message (unsigned char string [])
{
   printf("print string : %s \n", string);
}

int main (void)
{
   printf( "%s", string);
   return 0;
   }
hello.c: In function 'main':
hello.c:10:18: error: 'string' undeclared (first use in this function)
printf( "%s", string);
^~~~~~
hello.c:10:18: note: each undeclared identifier is reported only once for each function it appears in
 

be80be

Well-Known Member
You make a putch like Ian showed me
Code:
void  putch(char c)
   {
   USARTWrite(c);  // points to what is going to use it serial or lcd 
   }
It be like this
Code:
#include<stdio.h>
void  putch(char c)
   {
   USARTWrite(c);  // points to what is going to use it serial or lcd 
   }

int main (void)
}
your code here 
{
and you need this
That pdf is great
 

Attachments

be80be

Well-Known Member
You use it like this
Code:
while (1)
  {
   x = ADC_Read(0); //Reading Analog Channel 0  
   y = (x*5/1023);
  printf("%.2f \n",y); // this prints out like this 0.00 so 5 volts looks like this 5.00
       __delay_ms(500); // 1 Second Delay
  }
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Hi Burt!! We're still not sure what platform Parth is on... He may not be using XC8..

Parth!! "string" needs to be defined... But I don't know what you are doing... I'm guessing ( as usual)..

What do you want to print? printf() will print a string any way...

Assume string = " Hello World"... printf("%s", string); .. will print Hello World.. so will printf( "HelloWorld");

So what are you trying to do!!!!
 

be80be

Well-Known Member
He's trying to write code for LCD on a 8051 I think

He'd need something like this
Code:
void  putch(char c)
   {
   LCDWrite(c);  // points to what is going to use it serial or lcd
   }
Something like this would run it main

Code:
prinf("%s","Hello world")
Thanks Ian for showing how to use putch
 
Last edited:

Parth86

Member
Hi Burt!! We're still not sure what platform Parth is on... He may not be using XC8..
!
I have mention in post #3 I am using mingw gcc compiler on window 10
He's trying to write code for LCD on a 8051 I think
Thank to both of you for responding . yes I am trying to write program on keil compiler

I can write simple array string program but I am having problem in embedded program with basics
C:
#include<stdio.h>
int main (void)
{
    int i;
    char array[15] =  {"Hello Friends"};
    for(i = 0; i < 15;  i++)
    {
         printf("print array element : %c \n", array[i]);
    }
    return 0;

}
I get following output of this program
print array element : H
print array element : e
print array element : l
print array element : l
print array element : o
print array element :
print array element : F
print array element : r
print array element : i
print array element : e
print array element : n
print array element : d
print array element : s
print array element :
print array element :

I tried to write program

C:
#include<reg51.h>

#define port P1           /* Data pins connected to port P1 */

sbit RS = P2^0;           /* RS pin connected to pin 0 of port P2 */
sbit RW = P2^1;           /* RW pin connected to pin 1 of port P2 */
sbit EN = P2^2;           /* EN pin connected to pin 2 of port P2 */

/* Function to send command to LCD */
void LCD_Command(unsigned char cmd)
{
    port = cmd;
    RS = 0;
    RW = 0;
    EN = 1;
    Delay(2);
    EN = 0;
}
/*Function to send data to LCD */
void LCD_Data(unsigned char Data)
{
    port = Data;
    RS = 1;
    RW = 0;
    EN = 1;
    Delay(2);
    EN = 0;
}

/* function for delay */
    void Delay(unsigned int wait)
{
    unsigned i,j ;
    for(i = 0; i < wait; i++)
    for(j = 0; j < 1200; j++);
}

/* Function to prepare the LCD */
void LCD_init()
{
    LCD_Command(0x38);
    Delay(20);
    LCD_Command(0x0f);
    Delay(20);
    LCD_Command(0x01);
    Delay(20);
    LCD_Command(0x81);
    Delay(20);
}

void string (void)
{
     unsigned int i=0;

    unsigned char string[15]= "Hello Friend's";

     for(i=0; i<15; i++)
    {

    }

}
void main(void)
{
  LCD_init();
    while (1)
    {
          string ();
    }
}
I am facing problem in string function. I don't understand how to make this function for proper working.
 

Pommie

Well-Known Member
Most Helpful Member
Try writing it to the display.
Code:
void string (void)
     {
     unsigned int i=0;
    unsigned char string[15]= "Hello Friend's";
     for(i=0; i<15; i++)
    {
            LCD_Data(string[i]);
    }

}
Mike.
 

Pommie

Well-Known Member
Most Helpful Member
That is because you have it in a while(1) loop. Try putting string() before the while loop.

Mike.
 

dknguyen

Well-Known Member
Most Helpful Member
I have tried it but not getting what i want to display on screen
C:
 int main(void)
{
 
  LCD_init();
   string ();
      while (1)
      {
 
          }

}
You NEED to tell us what you want to see. You have not told us. You also NEED to tell us what you got with the above code and what was wrong with it. We can't tell you anything since we don't know what you are getting and we don't know what it is that you want.

Also, stop saying calling it a "string array" or an "array of strings". That is completely different. What you are talking about is a "string" or an "array of characters". Unless you actually are talking about an array of strings. What is it that you want? Because you seem to be talking about only one string, but you keep saying "array of strings" as well as
How to pass n string's as argument in function?
Are you talking about a string or array of characters that is n elements long? Or are you actually talking about an array of n array of characters?

Also, do not give your functions and array the same name string() and string[]. That's really bad practice. Give them different names. Give them real names. The only reason it is compiling here is that the variable is declared local to the function. Don't do it.

You pass a string, or an array of characters to a function like this:
Code:
void MyFunction(char InputText[]);
or
Code:
void MyFunction(char *InputText);
They can be used *almost* interchangeably. The main times you cannot use them interchangeably are when you do anything that needs to know the size of the array or it's elements such as when you use the sizeof() function or when you InputText++ or InputText+1 (because it needs to know to increment the address by the size of just one char element or the size of the entire array).

You call it with either any these, except the bottom one:
Code:
char MyText[15];

MyFunction(MyText);
MyFunction(&MyText);
MyFunction(&MyText[0]);
MyFunction(MyText[0]); //THIS IS WRONG because [] will dereference the address and not pass the address of the array. It will pass the value of the first element instead.
 
Last edited:

Parth86

Member
You NEED to tell us what you want to see. You have not told us. You also NEED to tell us what you got with the above code and what was wrong with it. We can't tell you anything since we don't know what you are getting and we don't know what it is that you want.
what I didn't mention ? Have you read post #12 and #14. there is all information

I want write program for LCD on a 8051. I want to see the string "hello friend " on my LCD screen

this is program

C:
#include<reg51.h>
#define port P1           /* Data pins connected to port P1 */
sbit RS = P2^0;           /* RS pin connected to pin 0 of port P2 */
sbit RW = P2^1;           /* RW pin connected to pin 1 of port P2 */
sbit EN = P2^2;           /* EN pin connected to pin 2 of port P2 */
   void Delay(unsigned int wait)
   {
           unsigned i,j ;
           for(i=0;i<wait;i++)
           for(j=0;j<1200;j++);
   }
/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    port = cmd;
    RS=0;
    RW=0;
    EN=1;
    Delay(2);
    EN=0;
}
/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
    port = Data;
    RS=1;
    RW=0;
    EN=1;
    Delay(2);
    EN=0;
}
/* function for delay */
/* Function to prepare the LCD */
void LCD_init()
{
    LCD_Command(0x38);
    Delay(20);
    LCD_Command(0x0f);
    Delay(20);
    LCD_Command(0x01);
    Delay(20);
    LCD_Command(0x81);
    Delay(20);
}

void string (void)
     {
     unsigned int i=0;
    unsigned char string[15]= "Hello Friend's";
     for(i=0; i<15; i++)
    {
            LCD_Data(string[i]);
    }

}
   int main(void)
{
 
  LCD_init();
   string ();
   while (1)
   {
     
   }

}
message on screen



message should be print on first row and it should be fixed
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top