Here is my current situation. I have a global array and it is originally declared as an int pointer. I malloc (100*sizeof(int)) to this pointer in the main method. I then use this array to store values and these values are assigned inside of two for loops. The problem is that as soon as I exit the loops and check the values they are all gone and equal zero. I confirm this with a printArray method that calls the prints the values of the array upon exiting the loops. I have included this below.

void compare(char *sub)
{
       extern char *target;
	int len;
	int scoreofseq;
	len = strlen(sub);
	extern size_t ll,sl;
	char *subspacer;
	subspacer=(char*)malloc(100*sizeof(char));
	strncpy(subspacer,sub+(ll+1),ll+sl);
	char first;
	first=subspacer[0];
	char last;
	last=subspacer[strlen(subspacer)-1];
	
       if((first==s1)&&(last==sN))
	{
		double gcount;
		gcount=gc(subspacer);
		
		if(gcount<.5)
		{
	                int pos;
			int j;
			int i;			
			char s;
			char *a;
			a=(char*)malloc(100*sizeof(char));	
			for ( pos=1, i=0; pos <= len; ++pos,i++)//This is the first loop 
			{
		
				s=sub[pos];
				strcpy(a,getMatches(target));
				printf("A is %s\n",a);
				for( j=0;j<strlen(a);j++)//This is the second loop
					{
						if (a[j]==s) 
						{
						match[j]=1;
					        
                                                }
						else 
						{
					       match[j]= 0;
						}		
					 /*When the print array was called here it printed out 1's where they were assigned*/
                                          }
			//Print array also printed out 1's here as well.
                              }
			printArray();// I call this and it prints out nothing but zeroes
		}
		scoreofseq=score();
		information.sc=scoreofseq;
		information.gcfr=gcount;
	
	}
free(subspacer);
}

The Print Array method

void printArray()
{
	int i;
	for(i=0; i<100;i++)
	{
		if(match[i] == 1)
		printf("Match %d = %d\n",i,match[i]);
		
	}
}

The array is globally assigned before the main method and the memory is allocated like so in the main method--> match=(int*)malloc(100*sizeof(int));

If you need me to include the main method I can but it was quite long and really the only thing that pertains to this is the malloc() call inside of it.Thanks for your time in advance!--Robert

this is the reason why globals are generally considered bad programming. unless you have a compelling reason to do so, do not use global variables.

pass your array as a pointer argument into the functions that will be making use of it and/or modifying it.

your problems will disappear.

*sigh. I was hoping that wasn't going to be the problem but I guess that is what I get for writing it that way and disregarding warnings regarding global variables. I will make the change in the morning and see if that corrects the problem. Thank you for your time btw.--Robert

This is currently what I have for my compare function.

void compare(char *sub,int match[],int size)

The method call I use is below

(sub,match[],100);

I then receive this error:
error: expected expression before ‘]’ token

If I pass match[100] as an argument , doesn't that make it just the value at match[100]. I thought I the proper way to pass an array as a function argument was a match[]. I assumed this was a pointer to the first element and it could be modified accordingly. Was I wrong?

If I pass match[100] as an argument , doesn't that make it just the value at match[100]. I thought I the proper way to pass an array as a function argument was a match[]. I assumed this was a pointer to the first element and it could be modified accordingly. Was I wrong?

Yes, you were wrong.

(sub,match,100);

should work. match is the 'pointer'. match[] is defining it as an array, use in the function definition, not part of the executable code.

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.