[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Problem passing arrays to functions

Hey guys I've got bit of a problem which has me stumped. It's most likely a stupid mistake but I've spent hours on it without any luck. The below piece of code should read in a text file with the below format:

A,B,12,34
C,V,54,65
C,F,686,9
...

This data should then be loaded into an array for future manipulation. On compiling the program and running the output displayed is essentially garbage. I'm using Borland C Compiler just in case there's a known bug though I doubt this is the case.

Any help is greatly appreciated, thanks.

Michael

#include <stdio.h>
#include <string.h>

#define MAX_NUM_PATHS 100
#define BUFFER_SIZE 20

/* Function prototypes */
int load_file(char filename[], char *path_data[][BUFFER_SIZE]);

int main(int argc, char **argv)
{
   char path_data[MAX_NUM_PATHS][BUFFER_SIZE];
   char filename[255];
   int num_paths, i;

   strcpy(filename, argv[1]);

   num_paths = load_file(filename, path_data);

   printf("%d paths were found in the file %s\n", num_paths, filename);

   /* Debug routine making sure the data was loaded correctly
   output should be the same as the input file.*/
   for (i = 0; i < num_paths; i++)
   {
      printf("%s", path_data[i]);
   }

   return 0;
}

int load_file(char filename[], char *path_data[][BUFFER_SIZE])
{
   int i;
   FILE *datafile;
   char buffer[BUFFER_SIZE];

   datafile = fopen(filename, "r");
   if (datafile == NULL)
   {
      return -1;
   }

   while (!feof(datafile))
   {
      fgets(buffer, BUFFER_SIZE, datafile);
      strcpy(&path_data[i], buffer);
      i++;
   }
   
   fclose(datafile);

   /* return the number of entries in the array/lines in the file. */
   return i - 1;

}

The complete file I'm attempting to load is:

H,V,36,75
V,I,41,80
I,T,58,85
T,V,45,55
P,T,20,85
P,K,60,70
V,W,22,75
W,P,22,70
K,W,82,60
I,N,31,85
N,O,41,80
N,C,26,75
O,S,19,75
S,D,14,85
D,L,24,80
D,C,45,80
L,C,39,80
D,R,18,85
S,A,96,70
R,B,29,85
B,M,40,85
M,S,59,80
R,M,34,85
0
miket6000
Asked:
miket6000
  • 3
  • 3
1 Solution
 
sunnycoderCommented:
Hi  miket6000,

char [] [] , char * [], char ** are NOT the same and you cannot use them interchangeably!!!!

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20823846.html
has the detailed explanations. If you still have some doubts, feel free to post back.

Cheers!
sunnycoder
0
 
miket6000Author Commented:
thanks sunnycoder, most of the information in the posted link was revision for me but your comment sparked a long lost memory that allowed me to fix the problem. changing the function declaration to:
int load_file(char filename[], char path_data[][BUFFER_SIZE])
solved the problem but I'm still a little fuzzy as to exactly how multi dimensional arrays are handled, specifically where char** comes into things. for example why can I not just use the following:

char filename[255];
filename = argv[1];

As I undersatnd it filename is a pointer and so to is argv[1], should filename not then be set to point to the start of the string recieved and put into argv[1]?

Thanks for your help,

Michael
0
 
sunnycoderCommented:
>As I undersatnd it filename is a pointer
No, it is not :) ... Array is not the same thing as a pointer ... it is just that compiler lets you use pointer notation with arrays and vice versa ...

In case of a pointer, there is an actual memory location for that variable which holds address of another location (which could be an array) ... In case of an array, there is just the allocated memory and no separate variable which points to it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
miket6000Author Commented:
Ok, I can live with that, then shouldn't filename = &argv[1] work? I know that it doesn't but surely there must be some similar assignment which does.
0
 
sunnycoderCommented:
char * filename = argv[1];
0
 
miket6000Author Commented:
Cool, thanks a lot for putting up with my not so intelligent questions, you've been a great help.

Michael.
0
 
varunhomeCommented:
Hi,
This is regarding the question asked at the very beginning of the post. I have written a detailed article on using multi-dimensional pointers in C++. It should be helpful to resolve all doubts unambiguously.

The issue here is that while compiler automatically allocates memory for arrays, memory for pointers must be dynamically allocated. The exact syntax for doing it is described in the article on my home page:

Article link:
http://www.geocities.com/varunhostel/TechnicalArticles/PointerArticle/PointerArticle_Intro.html

Some of the questions relevant to the post answered in the article are:

Chapter 9: How do I dynamically allocate memory for a 3-dimensional pointer equivalent to an array a[2][3][4]?

Chapter 6: To what extent can I inter-change pointer and array syntax in C++?

Chapter 11: How do I decipher very long function pointer declarations? Is there a standard approach for it?

Chapter 11: What is the difference between the two declarations: int *ptr[5] and int (*ptr) [5]?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now