Jump to content

what's wrong with this?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
martian

martian

    Newbie

  • Members
  • Pip
  • 9 posts
I don't know what's wrong with my code...

The function 'add' just doesn't return the variable 'sum'...


#include <stdio.h>

int add (int *p)

{

    int a, sum=0, i;

    for (a=0; a<5; a++)

    {

        sum=sum+*(p+a);

    }

    [COLOR="Blue"]return sum;[/COLOR]

}

int main()

{

    int i, ia[5]={56,36,21,48,76}, *ip;

    add (&ia[0]);

    getchar();

    return 0;

}


Please help me. Thanks~:)

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You need to assign a variable to add() in order to get the return value:

int main()
{
    int i, ia[5]={56,36,21,48,76}, *ip;
    int mySum =add (&ia[0]);
    getchar();
    return 0;
}


#3
martian

martian

    Newbie

  • Members
  • Pip
  • 9 posts
Thx a lot~~:D

#4
Programming Fan

Programming Fan

    Newbie

  • Members
  • PipPip
  • 25 posts

martian said:

I don't know what's wrong with my code...

The function 'add' just doesn't return the variable 'sum'...


#include <stdio.h>

int add (int *p)

{

    int a, sum=0, i;

    for (a=0; a<5; a++)

    {

        sum=sum+*(p+a);

    }

    [COLOR="Blue"]return sum;[/COLOR]

}

int main()

{

    int i, ia[5]={56,36,21,48,76}, *ip;

    add (&ia[0]);

    getchar();

    return 0;

}


Please help me. Thanks~:)

try like this ... you should to declare i=....


#include <stdio.h>

#include <iostream>


int add (int *p)


{

    int a, sum=0, i=0;

   

	for (a=0; a<5; a++)

    {

        sum=sum+*(p+a);

    }

    return sum;

}


int main()


{

   

	int i=0, ia[5]={56,36,21,48,76}, *ip=0;

   

	add (&ia[0]);

   

	getchar();

    system("pause");

	return 0;

}



#5
lucuis

lucuis

    Newbie

  • Members
  • PipPip
  • 21 posts
It returns, but you don't assign it to anything.

Also

add (&ia[0]);

can be written as

add (ia);


#6
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Really the big problem is that you're not outputting anything.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#7
martian

martian

    Newbie

  • Members
  • Pip
  • 9 posts
ic~Thx very much!:D