Thread: C thinks stuff inside a function is global?

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    3

    C thinks stuff inside a function is global?

    Hi all, I'd be very grateful if you could help me with a very strange set of errors and warnings I get on some C code. I've been staring at it for a while and I'm really frustrated.

    So, the code is like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void main(){
    int n,**mo,pn,m,p,n1=1,n2=1,test1[10][10]={{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
    p=phm(test1,&mo,n,n1,n2,&pn,&m);
    }
    int phm(int **h,int ***d_p,int n,int a1,int a2,int *k,int *pm){
        int **e1,**e2,**ma,**d;
        char pd,m;
        if((d=malloc(sizeof(int *)))==NULL)return(1);
        if((*d=malloc(sizeof(int)))==NULL)return(1);
        if(a1==a2){
            d={{a1}};
            pd=1;
        }
        e1={{a1}};
        e2={{a2}};
        m=0;
        while(!pd){
            *pm=1;
            d={{0}};
            pd=0;
        }
        *d_p=d;
        return(0);
    }
    Nevermind what it does or what it's supposed to do; this is sort of an abridged version of my program just to narrow down where the trouble arises. So I just want to get it to run, not give me any sensible results.

    Now the errors and warnings I get all seem to indicate that everything going on inside phm is taken to be global for some reason. Specifically, I get the following:

    Code:
    --------------------Configuration: mingw5 - CUI Debug, Builder Type: MinGW--------------------
    
    
    Checking file dependency...
    Compiling C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c...
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:3: warning: return type of 'main' is not `int'
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:13: error: syntax error before '{' token
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:14: warning: data definition has no type or storage class
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:15: error: syntax error before '}' token
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:16: warning: braces around scalar initializer
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:16: warning: (near initialization for `e1')
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:16: error: `a1' undeclared here (not in a function)
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:16: error: initializer element is not constant
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:16: error: (near initialization for `e1')
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:16: error: initializer element is not constant
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:16: error: (near initialization for `e1')
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:16: warning: data definition has no type or storage class
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:17: warning: braces around scalar initializer
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:17: warning: (near initialization for `e2')
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:17: error: `a2' undeclared here (not in a function)
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:17: error: initializer element is not constant
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:17: error: (near initialization for `e2')
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:17: error: initializer element is not constant
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:17: error: (near initialization for `e2')
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:17: warning: data definition has no type or storage class
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:18: warning: data definition has no type or storage class
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:19: error: syntax error before "while"
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:21: warning: initialization makes pointer from integer without a cast
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:21: warning: data definition has no type or storage class
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:22: warning: braces around scalar initializer
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:22: warning: (near initialization for `d')
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:22: warning: data definition has no type or storage class
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:23: error: redefinition of 'pd'
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:14: error: previous definition of 'pd' was here
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:23: warning: data definition has no type or storage class
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:24: error: syntax error before '}' token
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:25: warning: initialization makes pointer from integer without a cast
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:25: error: initializer element is not constant
    [Warning] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:25: warning: data definition has no type or storage class
    [Error] C:\Users\Owner\AppData\Roaming\C-Free\5.0\samples\CHello.c:26: error: syntax error before "return"
    
    
    Complete Make CHello: 18 error(s), 17 warning(s)


    These would all make total sense if all those commands were not inside any functions. But as it is, for the life of me I can't figure out what's happening. The brace pairs open and close as they should, so it's not a case of misplaced or missing braces. Same with parentheses. I'm going insane Please help? Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    main returns an int. Nothing is gained by making it void.
    You have no prototype for phm.
    test1 is not an int**. It is an int[][10].
    I have no idea what you think stuff like the following means so I can't help you with that.
    Code:
        d = {{a1}};
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2022
    Posts
    3
    Thanks for taking a look!

    Sure, I can make main() an int and add a prototype for phm. The command
    Code:
    d={{a1}}
    assigns to d the 2d array of dimensions 1x1 whose sole element is a1.

    I'm not sure I understand the difference between "int**" and "int[][10]". Have I made a mistake in the declaration? Do you happen to have a link handy?

    Thanks again

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    Why do you think that "C thinks stuff inside a function is global?"
    I don't get that impression at all.

    ... assigns to d the 2d array of dimensions 1x1 whose sole element is a1
    No it doesn't.

    As for the difference between a 2d array and a double pointer, consider:
    Code:
    int a[3][3]; // this is a 2d array of type int[][3] or int(*)[3]
    The expression a[1][2] is "syntactic sugar" for *(*(a + 1) + 2).
    In order for this pointer dereferencing to work, a must point to a block of 3 integers, i.e., it must be an int(*)[3]. If it was just a int** then there would be no associated information about the inner dimension size, so the expression wouldn't work.
    Last edited by john.c; 05-23-2022 at 02:39 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    May 2022
    Posts
    3
    Oh my God! Thank you!

    All I needed to do was declare test1 as int**, initialize it by

    Code:
    if((test1=malloc((sizeof(int *))*10))==NULL)exit(1);
    for(i=0;i<10;i++){
        if((test1[i]=malloc((sizeof(int))*10))==NULL)exit(1);
    }
    for(i=0;i<10;i++){
        for(j=0;j<10;j++){
            test1[i][j]=0;
        }
    }
    and replace all commands like
    Code:
    a={{b}}
    with
    Code:
    **a=b
    and it worked!

    I thought stuff was being interpreted as global because some error messages said "not in a function" and because it thought pd had to be a constant ("redefinition of 'pd'") and there's no reason to think that unless the assignment is global. (And I think some of the errors about initializer elements not being constants were referring to scalars?)

    Thank youuuuu
    Last edited by V_P; 05-23-2022 at 10:32 PM. Reason: A raccoon had messed with the forrmatting; the same raccoon replaced "i=0" and "j=0" with "i=1" and "j=1".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-01-2015, 10:55 AM
  2. Can't have Global Struct inside Global Struct
    By Pulse Cloud in forum C Programming
    Replies: 5
    Last Post: 10-05-2011, 03:41 PM
  3. Replies: 2
    Last Post: 04-02-2008, 06:30 AM
  4. Making global inside a block
    By siavoshkc in forum C++ Programming
    Replies: 17
    Last Post: 02-07-2006, 08:19 AM
  5. Who here thinks AI is interesting stuff.
    By Queatrix in forum General AI Programming
    Replies: 11
    Last Post: 04-23-2005, 10:41 PM

Tags for this Thread