Thread: error C2087: '<Unknown>' : missing subscript

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    error C2087: '<Unknown>' : missing subscript

    'initializing' : 'int ' differs in levels of indirection from 'void *'
    error C2087: '<Unknown>' : missing subscript
    warning C4013: 'malloc' undefined; assuming extern returning int
    warning C4047: 'return' : 'int ' differs in levels of indirection from 'int *'


    does anybody know what could be wrong, i checked everything....
    Code:
    int main()
    {
    	int sranje;
    	int matrica[100][100]={NULL};
    	int st, re,i,j;
    	printf("ST: ");
    	scanf("%d", &st);
    	printf("RE: ");
    	scanf("%d", &re);
    	for(i=0;i<st;i++)
    	{
    		for(j=0;j<re;j++)
    		{
    			printf("%d.%d = ",i+1,j+1);
    			scanf("%d", &matrica[i][j]);
    		}
    	}
    	for(i=0;i<re;i++)
    	{
    		printf("%d", funk(matrica,st,re));
    	}
    	scanf("%d", &sranje);
    }
    int funk(int matrica[][], int st, int re)
    {
    	int *niz;
    	int i,j,sum=0;
    	niz=((int *)malloc(sizeof(int)*(re)));
    	for(i=0;i<st;i++)
    	{
    		for(j=0;j<re;j++)
    		{
    			sum=sum+matrica[i][j];
    			niz[i]=sum;
    			if(j==st)
    			{
    				sum=0;
    			}
    		}
    	}
    	return niz;
    }
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Add "#include <stdlib.h>" would be a good start.

    You may want a prototype for "funk" too. And it returns "niz" which is a pointer to int, but you declare the function as "int funk(...)".

    You should alse free the return from funk at some point.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    nothing, wont work....i tried without pointers and malloc, with fixed size array, nothing...
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, I don't think you understood my comments.

    You need to give the compiler a clue as to what malloc does, which is to include stdlib.h

    If you allocate memory, you will need to give it back by calling free.

    By the way, your first error mesage is to do with this line:
    Code:
    	int matrica[100][100]={NULL};
    You are initializing a twodimensional array to NULL - NULL is a pointer, not an integer, so the compiler decides that this is not a valid initialization.

    And I presume that your "funk" is supposed to return an array, in which case you can't return a single integer value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    I'm not even using malloc anymore, not returning anything im printing output inside of function, things like this worked before without problem....this is stupid!

    Code:
    int funk(int matrica[][], int st, int re)
    {
    	int niz[50];
    	int i,j,sum=0;
    		for(i=0;i<st;i++)
    	{
    		for(j=0;j<re;j++)
    		{
    			sum=sum+matrica[i][j];
    			niz[i]=sum;
    			if(j==st)
    			{
    				sum=0;
    			}
    		}
    	}
    		for(i=0;i<re;i++)
    			printf("%d", niz[i]);
    }
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by shardin View Post
    I'm not even using malloc anymore, not returning anything im printing output inside of function, things like this worked before without problem....this is stupid!

    Code:
    int funk(int matrica[][], int st, int re)
    {
    	int niz[50];
    	int i,j,sum=0;
    		for(i=0;i<st;i++)
    	{
    		for(j=0;j<re;j++)
    		{
    			sum=sum+matrica[i][j];
    			niz[i]=sum;
    			if(j==st)
    			{
    				sum=0;
    			}
    		}
    	}
    		for(i=0;i<re;i++)
    			printf("%d", niz[i]);
    }
    I don't see anything wrong with that function. Describe what you mean by "not working anymore"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int funk(int matrica[][], int st, int re)
    You can't declare a multidimensional "array" as a function parameter without specifying the size of all dimensions except left-most.

    As an alternative you may consider int ** matrica.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int funk(int matrica[][100], int st, int re)
    When passing a multidimensional array in this manner, only the first dimension can be omitted.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    int funk(int matrica[][100], int st, int re)
    When passing a multidimensional array in this manner, only the first dimension can be omitted.
    Yes, of course. I so rarely pass 2D arrays around that I forget those little details.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM