7 Replies - 465 Views - Last Post: 17 June 2012 - 02:09 PM Rate Topic: -----

#1 vkambali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

function to return a character array to another function

Posted 17 June 2012 - 11:49 AM

i have to write two functions,one to convert prefix expression to postfix expression and pass the converted postfix expression to another function of postfix evaluation and display the result.i have written the code but there is some problem in passing.i am getting a warning as "conversion makes pointer from integer without a cast".i have attached my code
Is This A Good Question/Topic? 0
  • +

Replies To: function to return a character array to another function

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: function to return a character array to another function

Posted 17 June 2012 - 11:51 AM

Quote

i have written the code but there is some problem in passing.i am getting a warning as "conversion makes pointer from integer without a cast".i have attached my code


Where?

Please copy and paste your code into the post, inside of code tags.

:code:



Jim

This post has been edited by jimblumberg: 17 June 2012 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 vkambali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: function to return a character array to another function

Posted 17 June 2012 - 11:55 AM

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "MyDataType.h"
#define LENGTH 20
#include<ctype.h>
#define SIZE 50
#include<ctype.h>


void push(CHAR arrItem[LENGTH],INT *pTop,CHAR s[LENGTH][LENGTH])
{
        *pTop = *pTop + 1;
        strcpy(s[*pTop],arrItem);
}

void *pop(INT *pTop,CHAR s[LENGTH][LENGTH])
{
        CHAR *pItem;
        pItem = s[*pTop];
        *pTop = *pTop - 1;
        return pItem;
}

void pre_post(CHAR arrPrefix[LENGTH],CHAR arrPostfix[LENGTH])
{
        CHAR s[LENGTH][LENGTH];
        INT iTop = 0;
        INT iCount = 0;
        CHAR cSymbol;
        CHAR cTemp[LENGTH];
        CHAR cRev[LENGTH];
        CHAR *pOp1;
        CHAR *pOp2;
        iTop = -1;
        INT iCounter = 0;
        for(iCount = strlen(arrPrefix) - 1;iCount >= 0;iCount--)
        {
                cRev[iCounter++] = arrPrefix[iCount];
        }

        cRev[iCounter++] = '\0';
        printf("\n\nReverse of the expression is :%s",cRev);

        for(iCount = 0;iCount < strlen(cRev);iCount++)
        {
		cSymbol = cRev[iCount];
                cTemp[0] = cSymbol;
                cTemp[1] = '\0';
                if(!isdigit(cSymbol))
		{
                	pOp1 = pop(&iTop,s);
                        pOp2 = pop(&iTop,s);
                        strcpy(arrPostfix,pOp1);
                        strcat(arrPostfix,pOp2);
                        strcat(arrPostfix,cTemp);
                        push(arrPostfix,&iTop,s);
			return arrPostfix;
                }
		else
		{
                        push(cTemp,&iTop,s);
               	}
       
}
}


CHAR *post(CHAR arrPostfix[LENGTH])          
{

int s[SIZE];
int top=-1;       
CHAR ch;
int i=0,op1,op2; 
push(int elem)
{                       
 s[++top]=elem;
}
 

int pop()
{                      
 return(s[top--]);
}
                         
while( (ch=arrPostfix[i++]) != '\0')
 {
  if(isdigit(ch)) push(ch-'0');
  else
  {    
   op2=pop();
   op1=pop();
   switch(ch)
   {
   case '+':push(op1+op2);break;
   case '-':push(op1-op2);break;
   case '*':push(op1*op2);break;
   case '/':push(op1/op2);break;
   }
  }
 }
return (s[top]);
}

INT main()
{
        CHAR arrPrefix[LENGTH];
        CHAR arrPostfix[LENGTH];
        INT result;
        printf("\n\n Enter the prefix expression \n\n");
        scanf("%s",arrPrefix);
        printf("\n\nLength of the expression is %d",strlen(arrPrefix));
        pre_post(arrPrefix,arrPostfix);
        printf("\n\n The postfix expression is %s \n\n",arrPostfix);
}


View Postjimblumberg, on 17 June 2012 - 11:51 AM, said:

Quote

i have written the code but there is some problem in passing.i am getting a warning as "conversion makes pointer from integer without a cast".i have attached my code


Where?

Please copy and paste your code into the post, inside of code tags.

:code:



Jim


MOD EDIT: Removed redundant code and added code tags!!!!!!!!

This post has been edited by jimblumberg: 17 June 2012 - 12:00 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: function to return a character array to another function

Posted 17 June 2012 - 12:03 PM

Please post the contents of your "MyDataType.h" file, using code tags.

:code:


Jim
Was This Post Helpful? 0
  • +
  • -

#5 vkambali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: function to return a character array to another function

Posted 17 June 2012 - 12:16 PM

View Postjimblumberg, on 17 June 2012 - 12:03 PM, said:

Please post the contents of your "MyDataType.h" file, using code tags.

:code:


Jim



MyDataType.h contains

typedef int INT;
typedef char CHAR;

In main function 2 lines are missing that is to print the result of postfix expression evaluation and calling the post() function..i am not getting how to return the charcter array pass it to other function n and display the result in main.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: function to return a character array to another function

Posted 17 June 2012 - 12:32 PM

First stop with the silly typedefs. Learn the language properly before you start adding indirection.

Next when I compile your code I get the following errors:

Quote

main.c|13|warning: no previous declaration for ‘push’|
main.c|19|warning: no previous declaration for ‘pop’|
main.c|27|warning: no previous declaration for ‘pre_post’|
main.c||In function ‘pre_post’:|
main.c|47|warning: comparison between signed and unsigned integer expressions|
main.c|60|error: ‘return’ with a value, in function returning void|
main.c|71|warning: no previous declaration for ‘post’|
main.c||In function ‘post’:|
main.c|78|error: expected expression before ‘int’|
main.c|78|error: too few arguments to function ‘push’|
main.c|13|note: declared here|
main.c|79|error: expected ‘;’ before ‘{’ token|
main.c|77|warning: unused variable ‘op2’|
main.c|77|warning: unused variable ‘op1’|
main.c|77|warning: unused variable ‘i’|
main.c|76|warning: unused variable ‘ch’|
main.c|75|warning: unused variable ‘top’|
main.c|74|warning: unused variable ‘s’|
main.c|71|warning: unused parameter ‘arrPostfix’|
main.c||In function ‘main’:|
main.c|112|warning: unused variable ‘result’|
main.c||In function ‘post’:|
main.c|106|warning: control reaches end of non-void function|

You need to start a the first warning/error and fix that then recompile, and continue until you have no warnings or errors. Note that your error messages should tell you the file name where the error was detected along with the line number where the error was detected.

The "no previous declaration for XXX" messages are being caused because you did not provide a prototype for your functions. It is usually considered a good programming practice to always properly prototype your functions.

If you are not getting any warnings then you need to increase your compiler's warning level, or get a different compiler. Never ignore warnings, they are usually an early sign of problems to come.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 vkambali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: function to return a character array to another function

Posted 17 June 2012 - 01:34 PM

View Postjimblumberg, on 17 June 2012 - 12:32 PM, said:

First stop with the silly typedefs. Learn the language properly before you start adding indirection.

Next when I compile your code I get the following errors:

Quote

main.c|13|warning: no previous declaration for ‘push’|
main.c|19|warning: no previous declaration for ‘pop’|
main.c|27|warning: no previous declaration for ‘pre_post’|
main.c||In function ‘pre_post’:|
main.c|47|warning: comparison between signed and unsigned integer expressions|
main.c|60|error: ‘return’ with a value, in function returning void|
main.c|71|warning: no previous declaration for ‘post’|
main.c||In function ‘post’:|
main.c|78|error: expected expression before ‘int’|
main.c|78|error: too few arguments to function ‘push’|
main.c|13|note: declared here|
main.c|79|error: expected ‘;’ before ‘{’ token|
main.c|77|warning: unused variable ‘op2’|
main.c|77|warning: unused variable ‘op1’|
main.c|77|warning: unused variable ‘i’|
main.c|76|warning: unused variable ‘ch’|
main.c|75|warning: unused variable ‘top’|
main.c|74|warning: unused variable ‘s’|
main.c|71|warning: unused parameter ‘arrPostfix’|
main.c||In function ‘main’:|
main.c|112|warning: unused variable ‘result’|
main.c||In function ‘post’:|
main.c|106|warning: control reaches end of non-void function|

You need to start a the first warning/error and fix that then recompile, and continue until you have no warnings or errors. Note that your error messages should tell you the file name where the error was detected along with the line number where the error was detected.

The "no previous declaration for XXX" messages are being caused because you did not provide a prototype for your functions. It is usually considered a good programming practice to always properly prototype your functions.

If you are not getting any warnings then you need to increase your compiler's warning level, or get a different compiler. Never ignore warnings, they are usually an early sign of problems to come.

Jim



i have removed all the errors,have pasted the new code below, but there is one warning which i was not able to corect because as i said i dont know how to return a chaaracter array to another function (postfix evealuation function) and display the result in main..

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "MyDataType.h"
#define LENGTH 20
#include<ctype.h>
#define SIZE 50
#include<ctype.h>


void push(CHAR arrItem[LENGTH],INT *pTop,CHAR s[LENGTH][LENGTH])
{
        *pTop = *pTop + 1;
        strcpy(s[*pTop],arrItem);
}

void *pop(INT *pTop,CHAR s[LENGTH][LENGTH])
{
         CHAR *pItem;
        pItem = s[*pTop];
        *pTop = *pTop - 1;
        return pItem;
}

CHAR pre_post(CHAR arrPrefix[LENGTH],CHAR arrPostfix[LENGTH])
{
        CHAR s[LENGTH][LENGTH];
        INT iTop = 0;
        INT iCount = 0;
        CHAR cSymbol;
        CHAR cTemp[LENGTH];
        CHAR cRev[LENGTH];
        CHAR *pOp1;
        CHAR *pOp2;
        iTop = -1;
        INT iCounter = 0;
        for(iCount = strlen(arrPrefix) - 1;iCount >= 0;iCount--)
        {
                cRev[iCounter++] = arrPrefix[iCount];
                }

        cRev[iCounter++] = '\0';
        printf("\n\nReverse of the expression is :%s",cRev);

        for(iCount = 0;iCount < strlen(cRev);iCount++)
        {
                cSymbol = cRev[iCount];
                cTemp[0] = cSymbol;
                cTemp[1] = '\0';
                if(!isdigit(cSymbol))
                {
                        pOp1 = pop(&iTop,s);
                        pOp2 = pop(&iTop,s);
                        strcpy(arrPostfix,pOp1);
                        strcat(arrPostfix,pOp2);
                        strcat(arrPostfix,cTemp);
                        push(arrPostfix,&iTop,s);
                        return (arrPostfix);
                }
                else
                {
                        push(cTemp,&iTop,s);

                 }

}
}


CHAR post(CHAR arrPostfix[LENGTH])
{

int s[SIZE];
int top=-1;
CHAR ch;
int i=0,op1,op2;
void push(int elem)
{
 s[++top]=elem;
}


int pop()
{
 return(s[top--]);
}


while( (ch=arrPostfix[i++]) != '\0')
 {
  if(isdigit(ch)) push(ch-'0');
  else
  {
   op2=pop();
   op1=pop();
   switch(ch)
   {
   case '+':push(op1+op2);break;
   case '-':push(op1-op2);break;
   case '*':push(op1*op2);break;
   case '/':push(op1/op2);break;
   }
  }
 }
return (pop(s[top]));
}

INT main()
{
       INT Top,s[SIZE];
       CHAR arrPrefix[LENGTH];
        CHAR arrPostfix[LENGTH];
        INT result=s[Top];
        printf("\n\n Enter the prefix expression \n\n");
        scanf("%s",arrPrefix);
        printf("\n\nLength of the expression is %d",strlen(arrPrefix));
        pre_post(arrPrefix,arrPostfix);
        printf("\n\n The postfix expression is %s \n\n",arrPostfix);
        CHAR post(CHAR arrPostfix[LENGTH]);
        printf("Result of expression is :%d",result);
}

This post has been edited by jimblumberg: 17 June 2012 - 01:59 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: function to return a character array to another function

Posted 17 June 2012 - 02:09 PM

Quote

i have removed all the errors,

When I compile your code these are the messages I receive:

Quote

main.c|13|warning: no previous declaration for ‘push’|
main.c|19|warning: no previous declaration for ‘pop’|
main.c|27|warning: no previous declaration for ‘pre_post’|
main.c||In function ‘pre_post’:|
main.c|47|warning: comparison between signed and unsigned integer expressions|
main.c|60|error: return makes integer from pointer without a cast|
main.c|72|warning: no previous declaration for ‘post’|
main.c||In function ‘post’:|
main.c|79|error: ISO C forbids nested functions|
main.c|79|warning: no previous declaration for ‘push’|
main.c|79|warning: declaration of ‘push’ shadows a global declaration|
main.c|13|warning: shadowed declaration is here|
main.c|85|error: ISO C forbids nested functions|
main.c|85|warning: no previous declaration for ‘pop’|
main.c|85|warning: declaration of ‘pop’ shadows a global declaration|
main.c|19|warning: shadowed declaration is here|
main.c|98|warning: switch missing default case|
main.c||In function ‘main’:|
main.c|121|warning: redundant redeclaration of ‘post’|
main.c|72|note: previous definition of ‘post’ was here|
main.c||In function ‘pre_post’:|
main.c|69|warning: control reaches end of non-void function|
||=== Build finished: 4 errors, 14 warnings ===|


Quote

have pasted the new code below, but there is one warning which i was not able to corect because as i said i dont know how to return a chaaracter array to another function (postfix evealuation function) and display the result in main..

You don't return an array from a function, but you can return a pointer to an array.

In your function: void *pop(INT *pTop,CHAR s[LENGTH][LENGTH]) what do you actually think you are returning from this function?

I suggest that you study the excellent function tutorials provided in my signature. These tutorials should help answer about the use of function.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1