Hello there, I have an issue with execvp to a project in C i'm trying to procces..
Execvp is always returning -1.. and this warning " passing argument 2 of ‘execvp’ from incompatible pointer type" ..

here's the source code, and any advice at the matter would be appreciated!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>

#include <string.h>
  


int main(int argc, char *argv[])
{
   	FILE *fp1;
	FILE *fp2;
	char txtline[1000];
	int rows1,columns1,rows2,columns2,i,j;
	int pid;

		fp1=fopen("pinakas1.txt","r+");
		fscanf(fp1,"%d %d",&rows1,&columns1);

	int segment_idA = shmget(IPC_PRIVATE, rows1*columns1*(sizeof (int)), S_IRUSR | S_IWUSR);

	int *shared_memoryA=(int*) shmat (segment_idA , NULL, 0);

		for(i=0;i<rows1;i++)
		{

			for(j=0;j<columns1;j++)
			{
				fscanf(fp1,"%d",&shared_memoryA[i+j]);
				printf("%d ",shared_memoryA[i+j]);
			}
			printf("\n");
		}

		printf("\n");
		printf("\n");

		fp2=fopen("pinakas2.txt","r+");
		fscanf(fp2,"%d %d",&rows2,&columns2);

	int segment_idB = shmget(IPC_PRIVATE, rows2*columns2*(sizeof (int)), S_IRUSR | S_IWUSR);

	int *shared_memoryB=(int*) shmat (segment_idB , NULL, 0);


		for(i=0;i<rows2;i++)
		{

			for(j=0;j<columns2;j++)
			{
				fscanf(fp2,"%d",&shared_memoryB[i+j]);
				printf("%d ",shared_memoryB[i+j]);
			}
			printf("\n");
		}
		

		fp2=fopen("pinakas2.txt","r+");
		if(fp1==NULL|fp2==NULL)
		{
			printf("error: to arxeio pou oristike den yparxei i den einai prosbasimo gia anagnwsi\n");
			return 0;
		}


	
	int plithosDiergasiwn=rows1*columns2;
	int **C;
	
	C=(int**)malloc(rows1*sizeof(int *));
	for(i=0;i<rows1;i++)
	{
		C=(int**)malloc(columns1*sizeof(int));
		
	}
	
	int segment_idC = shmget(IPC_PRIVATE, plithosDiergasiwn*(sizeof (int)), S_IRUSR | S_IWUSR);

	int **shared_memoryC=(int**)shmat (segment_idC , NULL, 0);
   	printf("\n*** Forking... \n");
	
	int row=0,column=0;
	for(i=0;i<plithosDiergasiwn;i++)
	{
   	 	pid=fork();

   		if(pid==0)
		{
			
			printf("child no.%d\n",i);
			char args[7][50];
			char argspart[50];
printf("fork=%d\n",pid);
			sprintf(args[0],"%s","compute");

			printf("%s\n",args[0]);

			sprintf(args[1],"%d",segment_idA);
	
			printf("%s\n",args[1]);

			sprintf(args[2],"%d",segment_idB);

			printf("%s\n",args[2]);

			sprintf(args[3],"%d",segment_idC);
	
			printf("%s\n",args[3]);

			sprintf(args[4],"%d",0);
	
			printf("%s\n",args[4]);

			sprintf(args[5],"%d",0);
	
			printf("%s\n",args[5]);
			

			//sprintf(args[6],"%s",NULL);

			
			int X=execvp ("compute",args);
			printf("X=%d",X);
			break;
		}
		row++;
		column++;
		if(row==rows1)
		{
			row=0;
		}
		if(column==columns2)
		{
			column=0;
		}

	}



    if (pid>0) 
    {
     wait(0);
     printf("\n\nEnd of search. Returning to Parent process...\n");
    }
 
   if(pid<0)
   {
    printf("Fork failed...");
   }
    return 0;
}

Its a warning so it may work...Try casting the offending variable

int X=execvp ("compute",(char* const*)args);

char args[7][50] and char * args[7] are completely different creatures. The former is an array of 7*50 characters, while the latter is an array of 7 pointers.

A function foo taking char args[7][50] would have have a signature foo(char[][50]) ; it is vital to know the width of such array to work correctly. There's no way to pass such information to a function of exec family. You must hand them an array of pointers. For example,

char * real_args[7];
    real_args[0] = args[0];
    real_args[1] = args[1];
    // etc;
    execvp("compute", real_args);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.