Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the problem in this code?
I have this error
conflicting types for ‘random’void random(org select[size]){


What I have tried:

#include <stdio.h>    
#include <stdlib.h>   
#include <string.h>  
#define size 4

 typedef struct Org             		
{
   int  id[4]; 
   char name[4][7];  
}org;



struct buff
{
int bid[4];
char bname[4][7];
};
struct buff buf[3];

void random(org select[size]){



int i,j,r=0;
for (i=0; i< 3;i++){
       r = (rand() % (4 - 0)) + 0;
   for( j=0;j<6;j++){ 
    buf[i].bid[j]= select[r].id[j] ;
   strcpy(buf[i].bname[j], select[r].name[j]);
}}
for ( int i=0; i<4 ;i++){
     
   for(int j=0;j<4;j++){ 
printf("bname %s  bid = %d \n", buf[i].bname[j], buf[i].bid[j]);

}}

}

void main(  )
{
int i,j;
org select[size]; 

sprintf(select[0].name[0],"hello1");
sprintf(select[0].name[1],"hello2");
sprintf(select[0].name[2],"hello3");
sprintf(select[0].name[3],"hello4");

sprintf(select[1].name[0],"2ello1");
sprintf(select[1].name[1],"2ello2");
sprintf(select[1].name[2],"2ello3");
sprintf(select[1].name[3],"2ello4");

sprintf(select[2].name[0],"3ello1");
sprintf(select[2].name[1],"3ello2");
sprintf(select[2].name[2],"3ello3");
sprintf(select[2].name[3],"3ello4");

sprintf(select[3].name[0],"4ello1");
sprintf(select[3].name[1],"4ello2");
sprintf(select[3].name[2],"4ello3");
sprintf(select[3].name[3],"4ello4");

sprintf(select[4].name[0],"tello1");
sprintf(select[4].name[1],"tello2");
sprintf(select[4].name[2],"tello3");
sprintf(select[4].name[3],"tello4");





   printf(" Initial id :\n");
   for(i=0;i<4 ;i++)                         
    {
        for(j=0;j< 4;j++)            			
        { 
            // Rgene = (rand() % (5 - 1)) + 1;
             select[i].id[j]= j;  		
}}


random(select);

}
Posted
Updated 8-May-18 5:34am

You are trying to access the structure from a pointer to its first element, so it should be:
C++
void random(org* select){
 
Share this answer
 
Comments
neveen neveen 8-May-18 12:43pm    
what about the second struct buff ?
I define them like this org* select[size];
error:Segmentation fault
Richard MacCutchan 8-May-18 13:01pm    
Your structure definitions are all mixed up. You declare select with a length of 4, but then try to access 5 entries. You also have multiple entries for id and name inside your structures. It should be something like:

typedef struct Org
{
int id;
char name[7];
}org;

// ...

org select[size];

You can then refer to select indexed from 0 to size-1. So select[0].id, select[3].name etc.
neveen neveen 8-May-18 12:49pm    
the garbage value something like this cz��w^@hello2 , it must be hello1 and the id is correct, the garbage show just when use rand index
neveen neveen 8-May-18 13:26pm    
Can I copy just id according to r , and the name coped with it ?
Richard MacCutchan 8-May-18 14:31pm    
Yes, something like:

int index = 0;
select[index].id = 22;
sprintf(select[index].name, "hello1");
random is a standard function in the c-library. Use a different name, like myRandom or randomize to solve it.
 
Share this answer
 
Comments
neveen neveen 8-May-18 12:13pm    
thanks, Why sometimes I have garbage value when copy the name according to its index (r)?
Richard MacCutchan 8-May-18 12:30pm    
See my answer below.
Richard MacCutchan 8-May-18 12:31pm    
Actually the function name is rand().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900