C编程通过函数传递值

时间:2013-11-05 06:52:32

标签: c function variables

我正在尝试使用function2()来检查水平对和function3()以检查垂直对并将两个值传递回main以供function4()使用,我知道不能正确命名它们是不好的做法但是忍受我...任何帮助将不胜感激...

我的问题是为什么我为countX和countY获取0的输出以及如何修复它..

#include<stdio.h>
#include<stdlib.h>
#include<time.h>


#define maxrow 20  //defines maxrow as a constant of 20
#define maxcol 30  //defines maxcol as a constant of 30


void function1(char array[][maxcol]);
int function2(char array[][maxcol]);
int function3(char array[][maxcol]);
void function4(int, int); //function to display the pairs count

int main( void )
{ 

int countX = 0;
int countY = 0;
srandom( (unsigned) time(NULL) );
char array[maxrow][maxcol];

function1(array);

function2(array);

function3(array);

function4 (countX, countY);

    return ( 0 ) ;

} 


void function1(char array[][maxcol])
    {
    int x = 0;
    int y = 0;

     for (x=0;x<maxrow;x++)
        {
             for (y=0;y<maxcol;y++)
                {
                 array[x][y] = random() % 26 + 'A';
                 printf("%c ", array[x][y]);
                }
         printf("\n");
        }
    printf("\n");
    }
int function2(char array[][maxcol])
    {
    int col = 0;
    int row = 0;
    int countX = 0;

    for (row=0;row<maxrow-1;row++)
            {
            for (col=0;col<maxcol; col++)
                   {
                     if (array[row][col] == array[row+1][col])
                       {
                            countX++;
                       }
                   }
             }
    return(countX);
    }
int function3(char array[][maxcol])
    {
    int col = 0;
    int row = 0;
    int countY = 0;

    for(col=0;col<maxcol-1;col++)
        {
         for (row=0;row<maxrow;row++)
                {
                 if (array[row][col] == array[row][col+1])
                   {
                    countY++;
                   }
                }
        }
    return(countY);
    }
void function4(int countX, int countY)
    {
    printf("\nNumber of horizontal pairs: %d\n", countX);
    printf("\nNumber of vertical pairs: %d\n\n", countY);

    }    

输出:

N E R J B Z Y R I T Y J P P B G K R A Z X O A Y V W S E V V 
M L C F U D G S U O L U Z C M C K W T N V T B V T W U L B R 
I N C K U Y N A R K Q E G Q J S U T P Q G M J I K E E G P H 
X Z W C K Q A Z Q T L H Y S Z H K V A B L J O W R A A X G P 
H F R D H B U K A M D O V D G W K S S N U D W K C P K C O Q 
U V V L B F M X R O J U E F A K B K F V Z Z B V J D M V F D 
L B A I M B N A Y E R K B V P B I S N N O N O R K Z U X U Z 
C F D C N P G D S G J J Q K G H N Q C B F S O T J Y S D X M 
E Z T H E J Z M M R U V C L I K U V B Y W I Q M C B L W E K 
L L M E U Q P V E B O Y Z Q L J D G E G E D O X P S Y A P D 
L A Q X G M P W I T Z W T A P F J S L O A R R O O G H N J W 
S W Y K V E W K A E F C B Y C Q D M K Q C K I T A W B H L K 
F D I F N D K M N M Q U Q T T T L Y F V P H H X C I V D R J 
Q X O Y E E C Q Q R D G M V C H O N F V J U E Q T G A R M S 
A E R O E V S I M I A R R O M T V D I C Y R X D K S J K J X 
E L B V A I R U Q F F S Y Y G K T D N B G M V F R F Z C R J 
A W W D R W L K T E P A W P Y D A R I P U O D P V U W V X O 
G Z M C E F B S Q U W H U S X S X Z L G O F W U X S Q T P P 
H V O V X V B Y N R U J A O D X I B Y V H P D D J A X Z T M 
P D H F Y H A Z H N Q C Y T S C S B D R Y M G B R R B P Q V 


Number of horizontal pairs: 0

Number of vertical pairs: 0

3 个答案:

答案 0 :(得分:2)

原因如下:

function2(array); // in main
function3(array); //in main

这两个函数都返回,但在main中你只是调用它们而不收集返回的值。上面改为main()

 countX = function2(array); // this will put the value ( countX ) returned by this function in countX
 countY = function3(array); // this will put the value ( countY ) returned by this function in countY

答案 1 :(得分:1)

在函数function2function3中,变量countXcountY(分别)是 local 变量。就像countX中的countYmain main中的return一样。

解决问题的一种方法是使变量成为全局变量,但我不建议这样做。相反,我建议你main来自函数的变量,并在function2中分配给它们。

所以,例如在int function2(...) { ... return countX; }

main

countX = function2(...);

{{1}}

答案 2 :(得分:0)

制作countX和countY全局(并且不要在函数中重新声明)。

或者更好 - 在main()中:

countX = function2(array);
countY = function3(array);

免费提示:您在所有退货时都不需要() ......