0

I am getting errors in the following code. The errors disappear if I take out "struct point p2...". p1 is assembled the same way and works fine, what is the catch here?

#include <stdio.h>

struct point {
    int x;
    int y;
};

struct point makepoint(int x, int y)
{
    struct point temp;

    temp.x = x;
    temp.y = y;
    return temp;
}
struct point addpoint(struct point p1, struct point p2)
{
    p1.x += p2.x;
    p1.y += p2.y;
    return p1;
}

void main()
{
    struct point p1 = makepoint(5, 7);
    printf("p1 = (%d, %d)\n", p1.x, p1.y);
    struct point p2 = makepoint(2, 9);
    printf("p2 = (%d, %d)\n", p2.x, p2.y);
}
alk
  • 69,737
  • 10
  • 105
  • 255
kits
  • 609
  • 6
  • 20
  • 4
    Besides the signature of `main`, your code looks OK. – juanchopanza Dec 28 '14 at 15:57
  • 2
    What errors are you getting? – user12205 Dec 28 '14 at 15:57
  • 1
    In C, you're basically supposed to declare all variables first (so just move the declaration of `p2` one line upwards. – barak manos Dec 28 '14 at 15:58
  • Thank you barak...I wasted hours trying to figure out why this was happening. – kits Dec 28 '14 at 16:00
  • 1
    Out of curiosity, what compiler are you using? – user12205 Dec 28 '14 at 16:03
  • It is better to work with pointers, instead of struct values. I.e. convert `makepoint` to `void makepoint( int x, int y, struct point * pt )`. – i486 Dec 28 '14 at 16:05
  • he code has a very bad feature. Namely, passing of a whole struct as a return value and parameter values. This results in several undesirable events, such as at the return statement, the struct is copied (memcpy()) to a reserved-by-compiler area of memory. Then copied (memcpy()) to its' final destination in the caller. This results in two 'hidden' and unneeded calls to memcpy() and allocates a section of memory that cannot be used for anything else. suggest passing third parameter that is ptr to result struct and returning void or indication of success/failure – user3629249 Dec 28 '14 at 16:11
  • 6
    @barakmanos IIRC that only applies to C89. Since C99 is is possible to declare variables wherever you need them. – juanchopanza Dec 28 '14 at 16:27
  • @juanchopanza: Right... well obviously OP is using C89 (assuming I'm interpreting "Thank you barak...I wasted hours trying to figure out why this was happening" correctly). – barak manos Dec 28 '14 at 16:31
  • @barakmanos That looks likely. I am tempted to tag this as c89. – juanchopanza Dec 28 '14 at 16:32
  • @juanchopanza: I was tempted to answer this question, but then decided to delete the answer because I realized how many "Cxx standard says" comments I was going to receive. – barak manos Dec 28 '14 at 16:35
  • @barakmanos You can always qualify your answer with the information that it applies to C89, and that OP is most likely using that standard. – juanchopanza Dec 28 '14 at 16:37
  • How do I convert to C11 or C99? I am using Visual Studio 2012. – kits Dec 28 '14 at 16:39
  • @juanchopanza: Thanks, but I don't feel like it would be one of my notable answers, in which I would contribute my deep and unique knowledge. Once it becomes a "C-standard nit-picking" answer, I would prefer to skip it. – barak manos Dec 28 '14 at 16:40
  • 1
    @kits: You can't when sticking to VC. – alk Dec 28 '14 at 16:43
  • @kits: A quick hack would be to rename the file extension from `c` to `cpp`. – barak manos Dec 28 '14 at 16:45
  • 1
    You should have `int main()` instead of `void main()` and `return 0` before ending `main()` – Gopi Dec 28 '14 at 17:09
  • Please note that you would have gotten an answer quicker if you had shown the actual error messages that your compiler was giving you. We'd also have known that you were on Windows with MSVC. – Jonathan Leffler Dec 28 '14 at 17:16

3 Answers3

0
#include   <stdio.h>
#include   <string.h>
struct   arithmet   {
   char    eval;
   int       first_num,sec_num;
};

/*   function   declaration   */
void   eval_value(   struct   arithmet   *valyu   );
int   main(   )   {

   struct   arithmet   valyu1;               
   struct   arithmet   valyu2;
   char   oper;
   int   num1,num2;
   oper=getch();    //gets(oper);
   scanf("%d",&num1);
   scanf("%d",&num1);
   valyu1.first_num=num1;
   valyu2.sec_num=num2;
   valyu1.eval=oper;
   eval_value(&num1);
   eval_value(&num2);
   eval_value(&per);
   printBook(   &Book1   );
   printBook(   &Book2   );
   return   0;
}
void   eval_value(struct   arithmet   *valyu)
{
valyu->first_num;
valyu->sec_num;
valyu->oper;
if(oper=='+')
{
    printf("%d",addit(int   a,int   b));
}
if(oper=='-')
{
    printf("%d",subtractit(int   a,int   b));
}
if(oper=='*')
{
    printf("%d",multiplyit(int   a,int   b));
}
if(oper=='/')
{
    printf("%d",divideit(int   a,int   b));
}
if(oper=='@')
{
    printf("%d",intdivideit(int   a,int   b));
}
if(oper=='%')
{
    printf("%d",remdivideit(int   a,int   b));
}
if(oper=='~')
{
    printf("%d",exponeit(int   a,int   b));
}
}
  • Thank you for your contribution to the community. This may be a correct answer, but it’d be useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who may not be familiar with the syntax. Further, it can help reduce the need for follow-up questions. – Jeremy Caney May 06 '20 at 03:02
0
#include <stdio.h>

struct arithmetic {
 int x;
 int y;
 char oper;
 };

 int calcul(int a,int b)
{
 struct arithmetic temp;
int x,y;
char proc;
temp.x=x;
temp.y=y;
temp.proc=proc;
if(proc=='+')
{
  int addit(int x,int y)
    {
        return(x+y);
    }
 }
 if(proc=='-')
 {
  int devit(int x,int y)
    {
        return(x-y);
    }
}
  if(proc=='*')
 {
  int cumlatit(int x,int y)
     {

            return(x*y);
     }
   }
  if(proc=='/')
   {
    int dividit(int x,int y)
     {

            return(x/y);
     }
   }
  if(proc=='@')
  {
  int intdividit(int x,int y)
      {

          return(x/y);
      }
  }
  if(proc=='%')
   {
  int remdivit(int x,int y)
    {

        return(x%y);
    }
  }
  if(proc=='`')
  {
  int exponit(int x,int y)
    {
        int i=1,k=1;
        for(i=1;i<=y;i++)
            {
                k*=x;
            }
        return(k);
       }
  }
  void main()
  {
  struct arithmetic v1;
  int num1,num2;
  char proc;
  scanf(&num1);
  scanf(&num1);
  proc=getch();
  v1.x=num1;
  v1.y=num2;
  v1.oper=proc;
  struct calcul();
  }
-10
  • To avoid the many hidden calls to memcpy()
  • To avoid the many compiler allocated memory areas (that cannot be accessed/used by anything else.)

I suggest the following code, which compiles cleanly and exercises each function

#include <stdio.h>

struct point
{
    int x;
    int y;
};

void makepoint(int x, int y, struct point* newPoint)
{
    newPoint->x = x;
    newPoint->y = y;
} // end function: makepoint

void addpoint(struct point* p1, struct point* p2)
{
    p1->x += p2->x;
    p1->y += p2->y;
} // end function: addpoint

int main()
{
    struct point p1;
    struct point p2;

    makepoint(5, 7, &p1);
    printf("p1 = (%d, %d)\n", p1.x, p1.y);

    makepoint(2, 9, &p2);
    printf("p2 = (%d, %d)\n", p2.x, p2.y);

    addpoint( &p1, &p2 );
    printf("p1 = (%d, %d)\n", p1.x, p1.y);

    return(0);
} // end function: main
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3629249
  • 16,402
  • 1
  • 16
  • 17