How to pass the field of a struct to a function in a part library?

1

Hello, everyone.

I have a program that has a struct like this:

struct itemDeLista{ char nomeProd[10]; float quant; float vUnit; int item; };

But I need to count the number of letters in the stringProdName [10], as the teacher asked us not to use the string.h I'm doing the strlen function in a library of my own, I'm getting the char by scanf but when I step into the function of the library, some errors happen. I'll put the whole code.

#include <stdio.h>
#include <stdlib.h>
#include "biblio.h"

struct itemDeLista{
    char nomeProd[10];
    float quant;
    float vUnit;
    int item;
};

struct itemDeLista valores;

int main(){
  int k = 0;

  scanf("%[A-Z a-z]",valores.nomeProd);
  printf("%s\n", valores.nomeProd);
  k = strcnt(valores);
  printf("%d", k);
return(0);
}

This is the main program, the following is the library and function to count the number of characters.

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

int strcnt(struct itemDeLista m);

int strcnt(struct itemDeLista m){
   int i = 0, cont = 0;
   while(1){
      if(m.nomeProd[i] != '
#include <stdio.h>
#include <stdlib.h>
#include "biblio.h"

struct itemDeLista{
    char nomeProd[10];
    float quant;
    float vUnit;
    int item;
};

struct itemDeLista valores;

int main(){
  int k = 0;

  scanf("%[A-Z a-z]",valores.nomeProd);
  printf("%s\n", valores.nomeProd);
  k = strcnt(valores);
  printf("%d", k);
return(0);
}
'){ cont++; } else{ break; } i++; } return(cont); }

Thank you very much for the help, thanks!

    
asked by anonymous 19.08.2016 / 04:11

1 answer

1

With the definition of struct in the main program the library has no information about it.

You have to set the definition of struct itemDeLista to a file that both sources have access to; for example: List item.h

// itemDeLista.h
#ifndef ITEMDELISTA_H
#define ITEMDELISTA_H
struct itemDeLista {
    char nomeProd[10];
    float quant;
    float vUnit;
    int item;
};
#endif

Then include this header in your programs

#include <stdio.h>
#include <stdlib.h>
#include "biblio.h"
#include "itemDeLista.h"

struct itemDeLista valores;

int main(void) {
    int k = 0;

    scanf("%9[A-Z a-z]", valores.nomeProd);
    printf("%s\n", valores.nomeProd);
    k = strcnt(valores);
    printf("%d\n", k);
    return(0);
}

and in the source of your library

#include <stdlib.h>
#include <stdio.h>
#include "itemDeLista.h"

int strcnt(struct itemDeLista m);

int strcnt(struct itemDeLista m) {
    int i = 0, cont = 0;
    while (1) {
        if (m.nomeProd[i] != '
// itemDeLista.h
#ifndef ITEMDELISTA_H
#define ITEMDELISTA_H
struct itemDeLista {
    char nomeProd[10];
    float quant;
    float vUnit;
    int item;
};
#endif
') { cont++; } else { break; } i++; } return cont; }

If the header "biblio.h" already has the definition of struct you must include it in the library.

    
19.08.2016 / 11:04