LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2013, 12:25 PM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
problem in passing struct arrays to functions in C


I'm crazying because I'm not able to pass an array of structs to a function in C language.

All my code is following...

The main.c is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "funzioni.h"

int main() {

  int num;
  studente s[MAX];
  
  do {
    printf("Inserisci il numero di studenti da registrare: ");
    scanf("%d", &num);
  } while (num > MAX);
  
  inserisci_studenti(&s, num);
  visualizza_studenti(&s, num);

  return 0;
      
}
function.h is:
Code:
#define MAX 50

typedef char STRINGA[30];

typedef struct record_studente {
  STRINGA cognome;
  int voto;
} studente;

void inserisci_studenti(studente[], int);
void visualizza_studenti(studente[], int);
function.c is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "funzioni.h"

void inserisci_studenti(studente stud[], int dim) {

  int i;
  
  for (i = 0; i < dim; i++) {
    printf("%d)\n", i + 1);
    printf("Cognome: ");
    scanf("%s", stud[i].cognome);
    do {
      printf("Voto: ");
      scanf("%d", &stud[i].voto);
    } while (stud[i].voto < 0 || stud[i].voto > 30);
  }
  
}

void visualizza_studenti(studente stud[], int dim) {

  int i;
  
  printf("Visualizzazione degli studenti:\n");
  for (i = 0; i < dim; i++) {
    printf("Cognome: %s\n", stud[i].cognome);
    printf("Voto: %d\n", stud[i].voto);
    printf("\n");
  }

}
The error messages are:
main.c: In function ‘main’:
main.c:17:3: warning: passing argument 1 of ‘inserisci_studenti’ from incompatible pointer type [enabled by default]
In file included from main.c:3:0:
funzioni.h:13:6: note: expected ‘struct studente *’ but argument is of type ‘struct studente (*)[50]’
main.c:18:3: warning: passing argument 1 of ‘visualizza_studenti’ from incompatible pointer type [enabled by default]
In file included from main.c:3:0:
funzioni.h:14:6: note: expected ‘struct studente *’ but argument is of type ‘struct studente (*)[50]’
/tmp/ccfQtYvA.o: nella funzione "main":
main.c.text+0x45): riferimento non definito a "inserisci_studenti"
main.c.text+0x59): riferimento non definito a "visualizza_studenti"
collect2: error: ld returned 1 exit status

Please help me, I'm really becoming crazy!!

Thanks in advance.

Saverio
 
Old 10-15-2013, 12:50 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The way you have everything declared, I'm pretty sure you want to pass s rather than &s.

The linker error is because you have linked the object code from main.c without the object code from function.c. The simplest correction is to have both .c files in the same gcc command.

Last edited by johnsfine; 10-15-2013 at 12:52 PM.
 
Old 10-15-2013, 12:55 PM   #3
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Ok, it works, thank you very much!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C - How to properly pass a struct containing char arrays from one function to another LUB997 Programming 10 06-13-2013 02:23 AM
perl: Passing arrays to subroutine kenneho Programming 8 07-07-2009 06:14 AM
Passing bidimensional arrays to function in C pittopitto Programming 5 05-18-2006 09:31 AM
Arrays of Functions and Classes problem. (C++) -=zAe=- Programming 2 02-24-2005 10:21 AM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration