Like Tree1Likes

problem with array transport between programs

This is a discussion on problem with array transport between programs within the C Programming forums, part of the General Programming Boards category; I'm triyng to use an older program of mine as a function of a new one i made but instead ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    11

    problem with array transport between programs

    I'm triyng to use an older program of mine as a function of a new one i made but instead of just coping the code of the older to the new i wanted to use they as separeted files, i writed the codes i tought would be enough for this task and compilated the new one as main.c and the older as function.o with gcc but i'm geting a giant stray error, gonna post the coded and ask if any of you can help me, i'm doing this on linux.

    main.c

    Code:
    #include <stdio.h>  
    #include "function.o"
    #define N 4000
    
    void function( float Sx[], float h[], float Cor[][4], int PontosSpec, int v );
    
    
    int main()
    {
    
      int v, j,  NumeroEspectros, PontosSpec, q;
      char   L[N][1000];
      signed char   x[N][1000];
      float Sx[N], h[N], c[N], Cor[N][4];
    
    ...
    
         function(Sx, h, Cor, PontosSpec, v);
    
    ...
    }
    function.o

    Code:
      
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define N 4000
    void function( float Sx[], float h[], float Cor[][4], int PontosSpec, int v )
    {
      
      int i,j,q,NumeroPontosS, NumeroPontos ;
      float  y[N+1],p[N+1], o[N+1],l[N+1],z[N],x[N+1],a,b, Sy[N];
    
    ...
    
    Cor = ...;
    }
    I know the problem is in the link because both programs work fine when separated, i'm using "gcc main.c function.o -o prog" to compilate both in an executable, the awnser i need is the "Cor'' parameter, I'm getting errors like this in a lot of lines in the function when tryiyng to compilate:


    error: stray \374 in program
    error: stray \377 in program
    ...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That surely isn't your function.o file. Please tell me that's not your function.o file. If that is, rename it to function.c and stop lying to your C compiler.
    kpark91 likes this.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    Quote Originally Posted by tabstop View Post
    That surely isn't your function.o file. Please tell me that's not your function.o file. If that is, rename it to function.c and stop lying to your C compiler.
    If i try to make an executable with the main. and function.c files instead of main.c and function.o the result is the same.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    create a new file called function.h, add it to your project

    put this in function.h:

    Code:
    #define N 4000
    
    void function( float Sx[], float h[], float Cor[][4], int PontosSpec, int v );
    create a file called function.c, add it to your project

    put this in function.c:

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "function.h"
    
    #define N 4000
    
    void function( float Sx[], float h[], float Cor[][4], int PontosSpec, int v )
    {
      
      int i,j,q,NumeroPontosS, NumeroPontos ;
      float  y[N+1],p[N+1], o[N+1],l[N+1],z[N],x[N+1],a,b, Sy[N];
    
    ...
    
    Cor = ...;
    }
    Am not saying your code problems are over by doing this...
    Last edited by rogster001; 08-09-2011 at 08:55 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rogster001 View Post
    create a new file called function.h, add it to your project

    put this in function.h:

    Code:
    void function( float Sx[], float h[], float Cor[][4], int PontosSpec, int v );
    create a file called function.c, add it to your project

    put this in function.c:

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "function.h"
    
    #define N 4000
    
    void function( float Sx[], float h[], float Cor[][4], int PontosSpec, int v )
    {
      
      int i,j,q,NumeroPontosS, NumeroPontos ;
      float  y[N+1],p[N+1], o[N+1],l[N+1],z[N],x[N+1],a,b, Sy[N];
    
    ...
    
    Cor = ...;
    }
    ... then compile them separately into .o files and use LINK to combine them into an executable file.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    Do i erase the "#include "function.o" " from main.c? as long as this is there i get the same error.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    yes, i thought i showed that in the example code, replace it with function.h - files with an .o extension are objectfiles, an intermediate build file, you dont work with those directly, you need to create a header file as shown - .h

    in a header file you can put all the descriptions of your functions and data types, member variables they have, arguments the functions take.

    Then you include this header file in any of your source files that need to use the functions it describes - the function working code 'the function definitions. i.e. body of the function is contained in the source file function.c as shown
    Last edited by rogster001; 08-09-2011 at 09:24 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    Did what you told me, compiled them separated in .o files and them compiled them like this "gcc main.o function.o function.h -o prog", got the executable but the arrays are not geting to the function, i get 0.0 as awnser on the Cor parameter.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So maybe that's the answer.

    I'm surprised the thing even runs, with as many 4MB arrays as you're putting on the stack.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    Made an quick code to test what you guys are telling me, it didnt work either so i must be doing something wrong.

    Code:
    #include <stdio.h> 
    #include "del.h" 
    
    main()
    {
    
      int v;
    
    v=1;
    deli(v);
    printf("v = %d",v);
    }
    Code:
    #include <stdio.h> 
    #include "del.h" 
    
    
    void deli( int v )
    {
    v = v + 1;
    }
    Code:
    void deli( int v );
    compilated: gcc del.o deli.o del.h -o del

    im geting v = 1 as awnser wich means its not passing troght.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Am not saying your code problems are over by doing this...
    yep, ...

    Your v variable is only changed within the scope of the function it is passed to, you need to read up on scope, pointers, references

    try putting a printf statement inside the function you will soon see...
    Last edited by rogster001; 08-09-2011 at 09:46 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Notice that arrays are different in this respect: you can modify arrays inside a function (as they are passed by pointer), but not "normal" variables.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    Yeah, it worked which means my problems are worst than i thought, thank you anyway, you dont happen know any tutorial about this kind of operation do you?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've been very careful not to even hint at what operation you are doing, so I don't know how we could possibly suggest a tutorial.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by vtergolina View Post
    Do i erase the "#include "function.o" " from main.c? as long as this is there i get the same error.
    Yes. You cannot include object files. #include is intended mainly for header (.h) and source (.c) files with the latter being rarely used.

    Grab your C textbooks and start reading... the relationship between headers and source files should be explained there as well as the rules for scope and function calls.
    Last edited by CommonTater; 08-09-2011 at 10:11 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socketless Transport?
    By SMurf in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-20-2010, 02:04 PM
  2. Sockets Transport Question
    By SMurf in forum Tech Board
    Replies: 0
    Last Post: 12-14-2006, 04:36 PM
  3. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 03:02 AM
  4. N00bish problem: GUI programs
    By evermore96 in forum C Programming
    Replies: 2
    Last Post: 04-30-2005, 02:38 AM
  5. Replies: 3
    Last Post: 06-29-2003, 06:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21