Thread: using exec() to pass an array of numbers.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Unhappy using exec() to pass an array of numbers.

    hey guys, im having trouble using exec() to pass data to another porgram, i need to get a set of numbers from my first program and then use them in another program by using exec() so i can do any sort of operation.. any suggestions i tried looking everywhere cant find any examples

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    - Convert each number to a string
    - Assign each string to consecutive members of an argv
    - call exec


    Post your attempt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    i dont really undestand how does the other program uses the data sent i read about the main(argc,args[],){

    }

    getting the arguments.. but i dont know how would i use them

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Describing your problem in such vague terms is never going to work.

    Post some actual attempt, then we can probably tell you exactly how to fix it.

    We're not interested in "20 questions" posts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    lol of course i googled that, i cant find anyhting related to my question most examples are about passing arguments to use in the shell

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well show us what you wrote and what you would like to do specifically (i.e. I want to pass the string pointed at by char *x to program y) and we'll take it from there.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    well, what i want to do is pass 2 arrays to another program so it can do any sort of operation with them using exec(), i tried using a memory allocation but now i dont know how to send it, and then how does another program use what it gets.





    --------------------------------------------------
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct memoria {
        int i,j,m,n,h1,h2;
        int *mat1;
        int *mat2;
    };
    
    void* calcular (void* params){
            struct memoria * mem = (struct memoria *) params;
    
        
    
    }
    
    int main(){
    int i,j,m,n,x;
    struct memoria pol;
    while(1){
    printf("introduce el numero de filas y columnas de la matriz 1 y 2 en ese orden \n");
    	 	scanf("%d",&i);
    		scanf("%d",&j);
    
    		scanf("%d",&m);
    
    		scanf("%d",&n);
    
    					if(i!=n || j!=m || i<1 || j<1 || m<1 || n<1)
    							{
    								
    							printf("Matrices no multiplicables prueba de nuevo");	
    							}
    								else{	break;		}
    	}
    
    pol.i = i;
    pol.j = j;
    pol.m = m;
    pol.n = n;
    
    
    pol.mat1 = malloc( i*j*sizeof(int) );
    pol.mat2=malloc(n*m*sizeof(int));
    
    printf("introduce los datos de la matriz 1\n");
     					for(j=0;j<pol.j;j++){
      							for(i=0;i<pol.i;i++){
        									scanf("%d",&x);
    									pol.mat1[(j*pol.i)+i]=x;
      									}
    							 }
    printf("introduce los datos de la matriz 2\n");
     					for(m=0;m<pol.m;m++){
     							 for(n=0;n<pol.n;n++){
        										scanf("%d", &x);
    										pol.mat2[(m*pol.n)+n]=x;
     									 }
     							}
    
    
    					for(j=0;j<pol.j;j++){
      							for(i=0;i<pol.i;i++){
        									printf("%d ",pol.mat1[(j*pol.i)+i]);
      									}
    							printf("\n");
    							 }
     					for(m=0;m<pol.m;m++){
     							 for(n=0;n<pol.n;n++){
        										printf("%d ", pol.mat2[(m*pol.n)+n]);
     									 }
    							printf("\n");		
     							}
    
     return 0;
    }
    Last edited by Salem; 04-19-2010 at 11:25 AM. Reason: Added [code][/code] tags - learn to use them yourself - see the intro threads

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, EWWW on the indentation.
    Mixing spaces and hard tabs is a mess waiting to happen. Sooner or later, you'll use some tool (say HTML on a forum) which doesn't have the same idea about tabs as your clever IDE.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct memoria {
      int i,j,m,n,h1,h2;
      int *mat1;
      int *mat2;
    };
    
    void* calcular (void* params){
      struct memoria * mem = (struct memoria *) params;
    
      
    
    }
    
    int main(){
      int i,j,m,n,x;
      struct memoria pol;
      while(1){
        printf("introduce el numero de filas y columnas de la matriz 1 y 2 en ese orden \n");
        scanf("%d",&i);
        scanf("%d",&j);
    
        scanf("%d",&m);
    
        scanf("%d",&n);
    
        if(i!=n || j!=m || i<1 || j<1 || m<1 || n<1)
        {
          
          printf("Matrices no multiplicables prueba de nuevo");	
        }
        else{	break;		}
      }
    
      pol.i = i;
      pol.j = j;
      pol.m = m;
      pol.n = n;
    
    
      pol.mat1 = malloc( i*j*sizeof(int) );
      pol.mat2=malloc(n*m*sizeof(int));
    
      printf("introduce los datos de la matriz 1\n");
      for(j=0;j<pol.j;j++){
        for(i=0;i<pol.i;i++){
          scanf("%d",&x);
          pol.mat1[(j*pol.i)+i]=x;
        }
      }
      printf("introduce los datos de la matriz 2\n");
      for(m=0;m<pol.m;m++){
        for(n=0;n<pol.n;n++){
          scanf("%d", &x);
          pol.mat2[(m*pol.n)+n]=x;
        }
      }
    
    
      for(j=0;j<pol.j;j++){
        for(i=0;i<pol.i;i++){
          printf("%d ",pol.mat1[(j*pol.i)+i]);
        }
        printf("\n");
      }
      for(m=0;m<pol.m;m++){
        for(n=0;n<pol.n;n++){
          printf("%d ", pol.mat2[(m*pol.n)+n]);
        }
        printf("\n");		
      }
    
      return 0;
    }
    And to answer your question - no you can't use exec to pass your pol data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing a pair of numbers to an array
    By wankel in forum C Programming
    Replies: 53
    Last Post: 06-22-2009, 05:27 PM
  2. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  3. Standard Deviation of an array of numbers...
    By Xenofizz in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2003, 10:48 PM
  4. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM

Tags for this Thread