Thread: Calling an array into another in a function

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Calling an array into another in a function

    Hey all. Novice programmer, need help.

    I'm doing the Hamming code. My basic problem is that I know the logic of how to do this little part, but the syntax escapes me.

    I have two arrays, a "charbs[8]" and a "bits[15]". This function --

    Code:
    void correctTransmission(int wrongBit, int *theBit){
    	int corruptVal; 
    	int fixedVal;
    	
    	corruptVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
    
    	printf("Pre-CHAR corrupted value is: %d\n",corruptVal);
    	printf("Pre-CHAR fixed value is: %d\n",fixedVal);
    
    	printf("The corrupt value is: %c\n",(char)corruptVal);
    
    	flipBit(theBit,wrongBit);
    
    	fixedVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
    
    	printf("the corrected value is: %c\n",(char)fixedVal);
    
    }
    is apparently not working because I need to take-in the values of the charbs array into the bits array and work with that. Right now I'm getting all kinds of errors; almost all of the other functions work, but this one has been driving me insane for a good nine hours. It's like the bugs are mutating themselves into more and more evil. Usually this is awesome, but today this is not.

    Notes: This is part of a files seperate from my interface, called "functions.c". The main interface is presented below. A habit from Java, I guess.

    Code:
    #include<stdio.h>
    #include"functions.h"
    
    main()
    {
    	
    	/*void encodeChar (char c, int pointer);
    	void setCheckBits (int code[]);
    	void flipBit(int pointer, int bitNum);*/
    
    	char theChar;
    	int theNumber;
    	int flip;
    	int bits[15];
    	//int *pointer = bits;
    	//int *pointerChar = theChar;
    	int charbs[8];
    
    	printf("Please enter a character to be transmitted: ");
    	scanf("%c",&theChar);
    	encodeChar(theChar,charbs);	
    		
    	printf("Please enter a bit number (0-15, zero means no bit flipped): ");
    	scanf("%d",&theNumber);
    	flipBit(charbs,flip); //DECOMMENT later
    	
    	printArray(charbs,8); //this was added by Mike to help
    	printf("");
    	
    	correctTransmission(flip,bits);
    	//void checkBits(int *bits){
    	//bits[0]=bivoid correctTransmission(int wrongBit, int *theBit){
    	int corruptVal; 
    	int fixedVal;
    	
    	corruptVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
    
    	printf("Pre-CHAR corrupted value is: %d\n",corruptVal);
    	printf("Pre-CHAR fixed value is: %d\n",fixedVal); //time of work: 30+ hours now. :(
    
    	printf("The corrupt value is: %c\n",(char)corruptVal);
    
    	flipBit(theBit,wrongBit);
    
    	fixedVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
    
    	printf("the corrected value is: %c\n",(char)fixedVal);
    
    }
    		
    }
    Help, please. I'll post anything else you want me to.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    an uninitialized local variable has garbage in it, as in "fixedVal"

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Hmm! Now all I get are segmentation faults. Sorry for the size of this thing, I'm going to post it all:

    This is the main interface!
    Code:
    /* Assignment 2 1402 */
    
    #include<stdio.h>
    #include"functions.h"
    
    main()
    {
    	
    	/*void encodeChar (char c, int pointer);
    	void setCheckBits (int code[]);
    	void flipBit(int pointer, int bitNum);*/
    
    	char theChar;
    	int theNumber;
    	int flip;
    	int bits[15];
    	//int *pointer = bits;
    	//int *pointerChar = theChar;
    	int charbs[8];
    
    	printf("Please enter a character to be transmitted: ");
    	scanf("%c",&theChar);
    	encodeChar(theChar,charbs);	
    		
    	printf("Please enter a bit number (0-15, zero means no bit flipped): ");
    	scanf("%d",&theNumber);
    	//flipBit(charbs,flip); //DECOMMENT later
    	flipBit(bits,theNumber);	
    
    	decodeChar(charbs);
    	printArray(charbs,8); //this was added
    	printf("");
    		
    	correctTransmission(flip,bits);
    	
    		
    }
    This is the functions.c file:

    Code:
    #include<stdio.h>
    #include"functions.h"
    //I'll try to work you guys through this with comments.
    
    /*I'm calling a function to unpack the bits of the input character into an array of 15 ints, each of which
    stores a bit. I need to store the 8-character bits of input character into
    elements 3,5,6,7,9,10,11,12 of the ints array, and leaving other elements zero. Elements 13
    through 15 are "dummy" data and are going to be set to zero.*/
    
    void encodeChar (char theChar, int bits[]){
    	unsigned char temp;
    	unsigned char mask = 0x80;
    	int size=8;
    	int i;
    
    	for (i=0; i<size; i++){
    		temp = theChar & mask;
    		if (temp == 0)
    		{
    			bits[i]=0;
    			//printf("%d", temp);
    		}
    		else {
    			bits[i]=1;
    			//printf("%d", temp);
    		}
    	mask = mask >> 1;
    
    	}
    
    	//printf("\n");
    }
    
    //Call a function to set the 4 check bits...
    void setCheckBits(int *code){
    	code[0] = code[2] ^ code[4] ^ code[6] ^ code[8] ^ code[10] ^ code[12] ^ code[14];
    	code[1] = code[2] ^ code[5] ^ code[6] ^ code[9] ^ code[10] ^ code[13] ^ code[14];
    	code[3] = code[4] ^ code[5] ^ code[6] ^ code[11] ^ code[13] ^ code[14] ^ code[15];
    	code[7] = code[8] ^ code[9] ^ code[10] ^ code[11] ^ code[12] ^ code[13] ^ code[14];
    }
    
    /*The array is now assumed to be transmitted. OK, time to simulate noise. Call a function
    which gets as input the array and the number of the bit to be flipped in the array. This may be
    any of the 15 bits.*/
    void flipBit(int *numberArray, int flipped){
    	if(numberArray[flipped]==0) {
    		numberArray[flipped] = 1;
    	} else {
    		numberArray[flipped] = 0;
    	}
    
    }
    		
    /*Now call a function to simulate the error detection. The array gets passed to the function
    which returns an int with the incorrect bit number or zero if there are no errors.*/
    void clearArray(int *array, int size){
    	int i;
    
    	for (i=0; i<size; i++)
    	{
    		array[i]=0;
    	}
    }
    
    // Actually, I don't remember writing this. I'm not sure what it's doing here, or what it does.
    void printArray(int *array, int size){
    	int i;
    	char c;
    
    	for (i=0; i<size; i++)
    	{
    		printf("%d ", array[i]);
    	}
    
    	printf ("\n");
    }
    
    
    
    void correctTransmission(int wrongBit, int *theBit){
    	int corruptVal = 0; 
    	int fixedVal = 0;
    	
    	corruptVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
    
    	printf("Pre-CHAR corrupted value is: %d\n",corruptVal);
    	printf("Pre-CHAR fixed value is: %d\n",fixedVal);
    
    	printf("The corrupt value is: %c\n",(char)corruptVal);
    
    	flipBit(theBit,wrongBit);
    
    	fixedVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
    
    	printf("the corrected value is: %c\n",(char)fixedVal);
    
    }
    
    /*Now I'm supposed to call a function to simulate the error detection. The array gets passed to the function which returns an int with the incorrect bit number or zero if there are no errors.*/
    	
    int checkCode(int *bit) {
    	int brokenCode = (bit[0]*1) + (bit[1]*2) + (bit[3]*4) + (bit[7]*8);
    	return brokenCode;
    }
    
    #include<stdio.h>
    #include"functions.h"
    //void encodeChar (char c, int *code);
    //void setCheckBits (int code[]);
    //void flipBit(int *code, int bitNum);
    
    
    
    void flipBit(int *numberArray, int flipped){
    	if(numberArray[flipped]==0) {
    		numberArray[flipped] = 1;
    	} else {
    		numberArray[flipped] = 0;
    	}
    
    }
    
    void setCheckBits(int *code){
    	code[0] = code[2] ^ code[4] ^ code[6] ^ code[8] ^ code[10] ^ code[12] ^ code[14];
    	code[1] = code[2] ^ code[5] ^ code[6] ^ code[9] ^ code[10] ^ code[13] ^ code[14];
    	code[3] = code[4] ^ code[5] ^ code[6] ^ code[11] ^ code[13] ^ code[14] ^ code[15];
    	code[7] = code[8] ^ code[9] ^ code[10] ^ code[11] ^ code[12] ^ code[13] ^ code[14];
    }
    
    		
    /*
    void clearArray(int *array, int size){
    	int i;
    
    	for (i=0; i<size; i++)
    	{
    		array[i]=0;
    	}
    }*/
    
    void printArray(int *array, int size){
    	int i;
    	char c;
    
    	for (i=0; i<size; i++)
    	{
    		printf("%d ", array[i]);
    	}
    
    	printf ("\n");
    }
    
    void encodeChar (char theChar, int bits[]){
    	unsigned char temp;
    	unsigned char mask = 0x80;
    	int size=8;
    	int i;
    
    	for (i=0; i<size; i++){
    		temp = theChar & mask;
    		if (temp == 0)
    		{
    			bits[i]=0;
    			//printf("%d", temp);
    		}
    		else {
    			bits[i]=1;
    			//printf("%d", temp);
    		}
    	mask = mask >> 1;
    
    	}
    
    	//printf("\n");
    }
    
    void correctTransmission(int wrongBit, int *theBit){
    	int corruptVal = 0; 
    	//int fixedVal = 0;
    	
    	corruptVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
    
    	printf("Pre-CHAR corrupted value is: %d\n",corruptVal);
    	//printf("Pre-CHAR fixed value is: %d\n",fixedVal);
    
    	printf("The corrupt value is: %c\n",(char)corruptVal);
    
    	flipBit(theBit,wrongBit);
    
    	/*fixedVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);*/
    
    	//printf("the corrected value is: %c\n",(char)fixedVal);
    
    }
    	/*void addCode(int charBit, int *bit) {
    	int i = 0;
    	int bitsPosition = 2;
    
    	if (bitsPosition == 3) {
    		bitsPosition++;
    	}
    
    	if (bitsPosition == 7) {
    		bitsPosition++;
    	}
    
    	bit[bitsPosition] = charBit[i];
    	bitsPosition++;
    }*/
    
    int checkCode(int *bit) {
    	int brokenCode = (bit[0]*1) + (bit[1]*2) + (bit[3]*4) + (bit[7]*8);
    	return brokenCode;
    }
    
    char decodeChar(int *code) {
    	char fixedVal = (code[11]*1) + (code[10]*2) + (code[9]*4) + (code[8]*8) + (code[6]*16) + (code[5]*32) + (code[4]*64) + (code[2]*128); 
    	printf("the corrected value is: %c\n",fixedVal);
    }
    And finally this is the "functions.h" file:

    Code:
    void encodeChar (char theChar, int *numberArray); //yes
    void setCheckBits (int code[]); //yes
    void flipBit(int *numberArray, int bitNum); //yes
    int checkCode(int *code); //yes
    void addCode(int charBit, int *theBit);
    void correctTransmission(int wrongBit, int *theBit);
    // void clearArray(int pointer, int size);
    char decodeChar(int *code);
    Last edited by kroms; 02-28-2009 at 08:35 PM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    you have the same issue in main() where you call flipBit()
    Code:
    flipBit(charbs,flip); /* flip contains garbage */

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    But it's calling a function. Not sure how that works.

    I just updated my functions.c code.

    Edit: Also updated the Main file.

    My code still gives me a segmentation fault when I try to let it give me the corrupt function.
    Last edited by kroms; 02-28-2009 at 08:36 PM.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Which I believe brings us back to square one.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by kroms View Post
    But it's calling a function. Not sure how that works.

    I just updated my functions.c code.
    flip is a local variable and if it isn't initialized explicitly it will contain garbage and that's most likely what is happening in your code. Suggest setting flip to a value before passing it as an argument to flipBit(). Note that you are printing out values of int variables as "%c" which can't hold more than 127 char values so if the value goes beyond that it will print junk.

    Edit: post the updated code!
    Last edited by itCbitC; 02-28-2009 at 08:47 PM.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    A couple of things you can do to pin-point the source of error in your program:
    a. Trace the execution of the program by printing out stuff.
    b. Run it through a symbolic debugger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Calling a function with 2-D array
    By jmack549 in forum C Programming
    Replies: 1
    Last Post: 04-24-2009, 06:56 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM