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.

How to pass a int value and a string value to a function in c ?

Status
Not open for further replies.

thannara123

Advanced Member level 5
Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,602
Helped
122
Reputation
244
Reaction score
116
Trophy points
1,353
Visit site
Activity points
10,626
Hello Experts ,

I want to send an int value and a string value (using pointer) using a function .
i don't know well programming please help me .

I am giving my required function
Code:
InputNumber("Enter Password");  // [COLOR=#ff0000]string passing function  need to convert pass to both int and string value [/COLOR]

want to change the above program as follwos or something like that
Code:
InputNumber(reset,"Enter Password");  // [COLOR=#ff0000]reset is int value , enter password is string value
[/COLOR]
 

Hello!

It depends on your environment.
If you have access to stdio.h, you can write


Code C - [expand]
1
2
3
4
5
6
int InputNumber(char * string) {
    int retval; // Return value
    printf("%s : ");
    scanf("%d", &retval);
    return retval;
}




And then in your code, you write:
int reset;
reset = InputNumber("Enter password");

But it's strange to call this value reset. I would understand if you call it password or pass.

Dora.
 

Yes, reset can be put into the function. The function prototype and function definition has to be changed. How will be the reset used in the InputNumber() function?
Is it used to reset the values of some variables to 0? What value will you pass to reset?

New user_interface.c
Code:
#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>

#include "keypad.h"
#include "user_interface.h"
#include "lcd.h"

uint16_t InputNumber(uint8_t ireset, char *msg)
{
	uint8_t keyCode,x=0,pos = 0;
	uint8_t prev_key=0xFF;
	uint8_t k=0xc1;
	uint8_t system_reset = 0;
	uint8_t key[]="####";
	dis_cmd(0x81);
	//string("Enter Password");
	while(*msg) dis_data(*msg++);
     	LINE2;
	while(!system_reset)
	{		
		
		
		keyCode=GetKeyPressed(); 									//Get the keycode of pressed key

		if(keyCode==prev_key)
			continue;
		else if(keyCode==0xFF)
		{
			prev_key=0xFF;
			continue;
		}
		else
		{
			prev_key=keyCode;
		}

		if(pos==3)
		{
			return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3]));					//save the password in 16 bit integer
			//return(1234);
			break;
			//Wait for enter key or reset key
			//Convert pass to a single integer
			//
		}
		//
		
		switch (keyCode) //generating key characeter to display on LCD
		{
			case (11):
			string("1");	
			dis_cmd(k++);
			key[x]=1;
			++x;
			pos++;
			break;
	
	
			case (10):
			//dis_cmd(k++);
			string("2");	
			dis_cmd(k++);
			key[x]=2;
			++x;
			pos++;
			break;
	
			case (9):
			string("3");	
			dis_cmd(k++);
			key[x]=3;
			++x;
			pos++;
			break;
	
			case (7):
			string("nubere");
			dis_cmd(k++);
			key[x]=4;
			++x;
			pos++;
			break;
	
			case (6):
			//dis_cmd(k++);
			string("5");
			dis_cmd(k++);
			key[x]=5;
			++x;
			pos++;
			break;
	
			case (5):
			string("6");
			dis_cmd(k++);
			key[x]=6;
			++x;
			pos++;
			break;
	
			case (3):
			string("7");
			dis_cmd(k++);
			key[x]=7;
			++x;
			pos++;
			break;
		
			case (2):
			string("8");
			dis_cmd(k++);
			key[x]=8;
			++x;
			pos++;
			break;
	
			case (1):
			string("9");
			dis_cmd(k++);
			key[x]=9;
			++x;
			pos++;
			break;
	
			case (15):
			string("0");
			dis_cmd(k++);
			key[x]=0;
			++x;
			pos++;
			break;

			case (14):
			string("#");
			dis_cmd(k++);
			key[x]=0;
			++x;
			pos++;
			break;

			case (13):
			system_reset=1;
			break;
	
			default: ;

		}

		_delay_loop_2(1600);

	}
	//key[0]=1;key[1]=2;key[2]=3;key[3]=4;
		
		
	
}

New user interface.h
Code:
#ifndef USER_INTERFACE_H
#define USER_INTERFACE_H

#include <inttypes.h>

/*

Prompts the user to enter a 4 digit number

msg is a C style null terminated string for prompt.

eg. usage

int password;
password=InputNumber("Enter Password");

*/
uint16_t InputNumber(uint8_t ireset, char *msg);

#endif
 
Last edited:

Hi....
suppose ur string value is stored as
char *p="ABCD";
your integer value is stored as
int reset=23;
then values can be passed to the function as

InputNumber(reset,p);

on recieving side: [return type] InputNumber( reset, * string)

* if you want u can pass the integer value as pass by reference value as
InputNumber(&reset,p)

on recieving side: [return type] InputNumber( *reset, * string)
 

in prototyping a function do like this

int InputNumber(int ,char* );

if you can store the string into an array a[]="Enter Password";
in function

int InputNumber(reset,a)
{

}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top