Returning values through pointers in functions.

Hey guys, I'm new to c I've been having trouble with my understanding of this stuff; how would I go about returning three values to my main from a function using pointers?
I've tried looking it up, but I was unsuccessful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
void filestats(FILE*,float*,float*,float*);

int main (void)
{
    float nl=0,nw=0,nc=0;
    float *nl_ptr,*nw_ptr,*nc_ptr;                         //THIS IS WHERE MY TROUBLE IS
    nl_ptr=&nl;nw_ptr=&nw;nc_ptr=&nc;                         //THIS IS WHERE MY TROUBLE IS
    FILE *fin=fopen("filestatsin.txt","r");
    filestats(fin,&nl,&nw,&nc);                         //THIS IS WHERE MY TROUBLE IS
    
    printf("chars: %d words: %d lines:%d",nc,nw,*nl_ptr);                         //THIS IS WHERE MY TROUBLE IS
    system("pause");
    return 0;
}
void filestats(FILE * fin,float *nl_ptr,float *nw_ptr,float *nc_ptr)
{
    char c;
    int l=0,w=0,c2=0;
    while(fscanf(fin,"%c",&c)!=EOF){
        printf("%c",c);
        if((c=='a')||(c=='A')||(c=='b')||(c=='B')||(c=='c')||(c=='C')||(c=='d')||(c=='D')||(c=='e')||(c=='E')||(c=='f')||(c=='F')||(c=='g')||(c=='G')||(c=='h')||(c=='H')||(c=='i')||(c=='I')||(c=='j')||(c=='J')||(c=='k')||(c=='K')||(c=='l')||(c=='L')||(c=='m')||(c=='M')||(c=='n')||(c=='N')||(c=='o')||(c=='O')||(c=='p')||(c=='P')||(c=='q')||(c=='Q')||(c=='r')||(c=='R')||(c=='s')||(c=='S')||(c=='t')||(c=='T')||(c=='u')||(c=='U')||(c=='v')||(c=='V')||(c=='w')||(c=='W')||(c=='x')||(c=='X')||(c=='y')||(c=='Y')||(c=='z')||(c=='Z')){
            c2++;
        }else if((c==' ')){
            w++;
        }else if(c=='\n'){
            l++;
        }
    }
    *nw_ptr=(float)w+(float)l;                         //THIS IS WHERE MY TROUBLE IS
    *nl_ptr=(float)l;                         //THIS IS WHERE MY TROUBLE IS
    *nc_ptr=(float)c2;                         //THIS IS WHERE MY TROUBLE IS
    printf("chars: %d words: %d lines:%d",c2,w+l,l);
    system("pause");
    return;
}
You don't return them. That's the whole idea. The pointer points at the variable that exists in the function above (i.e. in main) and you use the pointer to change the variables.

I've used the C++ iostream for this to output the results. The pointer related code is C code as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

void changeMyVars(int* pointer1, int* pointer2 , int* pointer3)
{
  (*pointer1)++; // change the variable that is being pointed at
  (*pointer2)++;
  (*pointer3)++;
}


int main()
{
  int x=3;
  int y=4;
  int z=5;
  cout << "x=" << x << " y=" << y << " z= " << z << endl;
  changeMyVars(&x, &y, &z); // pass in pointers to variables
  cout << "x=" << x << " y=" << y << " z= " << z << endl;
}


While I'm here, this:
if((c=='a')||(c=='A')||(c=='b')||(c=='B')||(c=='c')||(c=='C')||(c=='d')||(c=='D')||(c=='e')||(c=='E')||(c=='f')||(c=='F')||(c=='g')||(c=='G')||(c=='h')||(c=='H')||(c=='i')||(c=='I')||(c=='j')||(c=='J')||(c=='k')||(c=='K')||(c=='l')||(c=='L')||(c=='m')||(c=='M')||(c=='n')||(c=='N')||(c=='o')||(c=='O')||(c=='p')||(c=='P')||(c=='q')||(c=='Q')||(c=='r')||(c=='R')||(c=='s')||(c=='S')||(c=='t')||(c=='T')||(c=='u')||(c=='U')||(c=='v')||(c=='V')||(c=='w')||(c=='W')||(c=='x')||(c=='X')||(c=='y')||(c=='Y')||(c=='z')||(c=='Z'))
is more commonly done like this:
if( isalpha(c) )
Last edited on
Thank you so much, but I realized that I had made a mistake. (That's coding for you I guess.)

As for the isalpha(c) function, what would I need to include to have that defined?
#include "ctype.h" to use it.
Topic archived. No new replies allowed.