4 Replies - 169 Views - Last Post: 20 September 2012 - 05:38 AM Rate Topic: -----

#1 TIFunkalicious  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 20-September 12

2 Different versions of the same program will compile, 1 won't run

Posted 20 September 2012 - 01:54 AM

This program currently accepts files of indeterminate size that include a square block of characters and a word list. It is supposed to solve a wordsearch but that's not my current problem. I first created a simple version of the program that read the input file as stdin using the < operator in my class' unix shell that I am accessing through PuTTY. I copied it into a large 2d array, and copied that 2d array into 2 smaller arrays containing just the grid and the word list. The first program I am going to post WORKS PERFECTLY FINE.

Enter the second version. This one has 1 more function that does essentially nothing, a new pointer (length_ptr), and changes to parameters to use this pointer. This second version will compile but when ran does nothing. I have entered a printf statement at the very beginning of the main function and it shows this. I then entered a printf statement after the first declaration (char row[LINE]) and it DOESNT show this.

VERSION 1, THIS WORKS FINE
#include <stdio.h>
#include <stdlib.h>

#define LINE 101
#define SIZE 150
#define WIDTH 50
void process(char * row, char * inputGrid, char * grid, char * wordList, int * words);

int main()
{
char row[LINE];
char inputGrid[SIZE][LINE];
char grid[50][LINE];
char wordList[LINE][WIDTH];

char * row_ptr = row;
char * inputGrid_ptr = &inputGrid[0][0];
char * grid_ptr = &grid[0][0];
char * wordList_ptr = &wordList[0][0];

int words;
int * words_ptr = &words;

process(row_ptr, inputGrid_ptr, grid_ptr, wordList_ptr, words_ptr);

return 0;
}

void process(char * row, char * inputGrid, char * grid, char * wordList, int * words)
{
int i = 0;
int x = 0;
int length = 0;
int height = 0;

	for(i = 0; i < SIZE; i++)
	{
		if(fgets(row, LINE, stdin) == NULL) break;
		height++;
		for(x = 0; x < LINE; x++)
		{
			*(inputGrid + x + (LINE * i)) = row[x];
			if (row[x] == '\n') break;
		}
	}

	do{
		length++;
	}while(*(inputGrid + length) != '\n');

	*words = height - (length/2);
	printf("\nGrid: \n");
	for(i = 0; i < (length/2); i++)
	{
		for(x = 0; x < (length + 1); x++)
		{
			*(grid + x + (LINE * i)) = *(inputGrid + x + (LINE * i));
			printf("%c", *(grid + x + (LINE * i)));
		}
	}
	printf("\nWord List: \n");
	for(i = (height - *words); i < height; i++)
	{
		for(x = 0; x < (length + 1); x++)
		{
			*(wordList + x + (LINE * i)) = *(inputGrid + x + (LINE * i));
			printf("%c", *(wordList + x + (LINE * i)));
		}
	}
}


VERSION 2, THIS WILL NOT RUN PROPERLY
#include <stdio.h>
#include <stdlib.h>

#define LINE 101
#define SIZE 150
#define WIDTH 50

void process(char * row, char * inputGrid, char * grid, char * wordList, int * words, int * length);
void solve(char * row, char * inputGrid, char * grid, char * wordList, int * words, int * length);

int main()
{
char row[LINE];
char inputGrid[SIZE][LINE];
char grid[50][LINE];
char wordList[LINE][WIDTH];

char * row_ptr = row;
char * inputGrid_ptr = &inputGrid[0][0];
char * grid_ptr = &grid[0][0];
char * wordList_ptr = &wordList[0][0];

int words;
int * words_ptr = &words;
int length;
int * length_ptr = &length;

process(row_ptr, inputGrid_ptr, grid_ptr, wordList_ptr, words_ptr, length_ptr);
solve  (row_ptr, inputGrid_ptr, grid_ptr, wordList_ptr, words_ptr, length_ptr);

return 0;
}

void process(char * row, char * inputGrid, char * grid, char * wordList, int * words, int * length)
{
int i = 0;
int x = 0;
int height = 0;
int length_var = *length;

	for(i = 0; i < SIZE; i++)
	{
		if(fgets(row, LINE, stdin) == NULL) break;
		height++;
		for(x = 0; x < LINE; x++)
		{
			*(inputGrid + x + (LINE * i)) = row[x];
			if (row[x] == '\n') break;
		}
	}

	
    do{
		*length = *length + 1;
	}while((*(inputGrid) + length_var) != '\n');
	length_var = *length;

	
    *words = height - (length_var/2);
    
    printf("\nGrid: \n");

	
    for(i = 0; i < (length_var/2); i++)
	{
		for(x = 0; x < (length_var + 1); x++)
		{
			*(grid + x + (LINE * i)) = *(inputGrid + x + (LINE * i));
			printf("%c", *(grid + x + (LINE * i)));
		}
	}
	
    
    printf("\nWord List: \n");
	
    
    for(i = (height - *words); i < height; i++)
	{
		for(x = 0; x < (length_var + 1); x++)
		{
			*(wordList + x + (LINE * i)) = *(inputGrid + x + (LINE * i));
			printf("%c", *(wordList + x + (LINE * i)));
		}
	}
}

void solve(char * row, char * inputGrid, char * grid, char * wordList, int * words, int * length)
{
int i = 0;
int x = 0;
int flag[50][LINE];
}


This post has been edited by Salem_c: 20 September 2012 - 02:35 AM
Reason for edit:: adjust font size - 7 is too much


Is This A Good Question/Topic? 0
  • +

Replies To: 2 Different versions of the same program will compile, 1 won't run

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: 2 Different versions of the same program will compile, 1 won't run

Posted 20 September 2012 - 02:48 AM

This bit
    do{
        length++;
    }while(*(inputGrid + length) != '\n');



Is not the same as this bit
    do{
        *length = *length + 1;
    }while((*(inputGrid) + length_var) != '\n');
    length_var = *length;


length_var doesn't change, so this loop just locks up.
Further, it seems that *length is not initialised anyway.


Another big question for me is why you're flattening all your 2D arrays, and making such horrid self-subscript code like this.
*(wordList + x + (LINE * i)) = *(inputGrid + x + (LINE * i));
Do you know how to pass 2D arrays to a function, and access them as 2D arrays?

If not, we can explain, and make your code a hell of a lot simpler.
Was This Post Helpful? 1
  • +
  • -

#3 TIFunkalicious  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 20-September 12

Re: 2 Different versions of the same program will compile, 1 won't run

Posted 20 September 2012 - 04:21 AM

Honestly I don't know about passing 2d arrays and would love a breakdown. Once i read that there was no difference memorywise between 2d arrays and long single arrays I just never looked for a better way.
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: 2 Different versions of the same program will compile, 1 won't run

Posted 20 September 2012 - 04:38 AM

Let's say that you have this array
char inputGrid[SIZE][LINE];

The super easy way of declaring your function to receive this array is by copy/paste.

// prototype
void process ( char inputGrid[SIZE][LINE] );

// definition
void process ( char inputGrid[SIZE][LINE] ) {
  // now use it as you would normally
  inputGrid[0][0] = 'a';
  fgets( inputGrid[0], LINE, stdin );
  // and so on
}



Calling it is the same as for a 1D array. It's just the array name.
process( inputGrid );


Advanced Notes:
Strictly speaking, the left-most dimension of an array in a function prototype is ignored.
Sometimes, compilers (and other code checking tools) will remind you of this fact.

So a final edit is to remove the left-most dimension, like so.
void process ( char inputGrid[][LINE] );


Bonus Round:
To emphasise the fact that the parameter type is a pointer to an array, you can also write it as
void process ( char (*inputGrid)[LINE] );

Since all three forms are equivalent, this last one is perhaps too many edits to bother with.
Especially since the () are very important. Forgetting them yields char *inputGrid[LINE], which is also valid syntax, but a completely different kind of array.
Was This Post Helpful? 0
  • +
  • -

#5 TIFunkalicious  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 20-September 12

Re: 2 Different versions of the same program will compile, 1 won't run

Posted 20 September 2012 - 05:38 AM

I found the real problem, in the dowhile loop i was strangely dereferencing inputGrid before the addition which forced nothing to happen, I don't remember putting parentheses around it or why but removing them and fixing the loop as mentioned made it produce identical output

Thanks for your help I will definitely have to rewrite this for the 2D arrays because I will be passing and using them a lot more before this program is finished
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1