Sign in to follow this  
jmpeer

[Question] Array Parameters

Recommended Posts

Hey, I have a question. I wrote a small C program, but I'm having trouble with some concept here. Please take a look at the code real fast. I basically wanted to make MyArray, pass it into MyFunction which will manipulate it, then use the new values in main. I printed MyArray in MyFunction, it prints out fine. I printed MyArray in main after I called MyFunction, and it prints out garbage. T_T So I assume it has something to do with pointers? Or arrays as parameters? Help would be much appreciated ^_^
#include <stdio.h>

void MyFunction(char* MyArray[])
{
    int i;
    for(i=0; i<10; i++)
    {
        MyArray[i]="Meaningless Content";
        printf("%s\n",MyArray[i]);           //This is where it works.
    }
}
int main()
{
    char* MyArray[10][50];
    MyFunction(MyArray);
    int i;
    for(i=0; i<10; i++)                     
    {                                       
        printf("%s\n",MyArray[i]);          //This is where it doesn't work.
    }
    return 0;
}

Share this post


Link to post
Share on other sites
There's two problems here:

  • MyFunction does not know that the array you're passing in is multidimensional, and is instead treating the array as 1-dimensional, storing values contiguously.
  • Your array char* MyArray[10][50] is confused: It appears that you're trying to reserve storage for the characters in the array ([50]), but having the array store char *s, instead of just chars.


You can fix both of these issues by changing your MyArray type to char* MyArray[10].

Quote:
Original post by vaneger
You want to pass by reference instead of by pointer.
*** Source Snippet Removed ***

C, not C++ :) - besides, that wouldn't help.

Share this post


Link to post
Share on other sites

char v1; // A character
char* v2; // A pointer to character
char v3[50] // Array of character
char v4[10][50] // Two-dimesional array of character
char* v5[10][50] // Two-dimesional array of pointer to character


You can not assign a string to a raw character array like this
v4[i] = "Meaningless Content"
Even if the compiler will accept it, the = operator does not know how to copy the content of one array into another array.

You need to use a function like strcpy
strcpy(v4[i], "Meaningless Content");
The strcpy function will typically loop throught the string copying one character at a time

Share this post


Link to post
Share on other sites
[SOLVED]

Ah, I got it now.

I was trying to read a text file, seperate the lines into an array, manipulate
them, and save them as another file.

My problem was with the parameter. I needed to specify the size of the second
dimension [50].

Your suggestions did fix my example.
Here is the actual source code I had a problem with.
It's obviously incomplete, but now it doesn't have a problem reading the
text file into the array.

I'm trying to become more familiar with functions from the C Standard Library.
If you have any suggestions about this code, please feel free to contribute.


#include <stdio.h>

int GetNumLines(FILE* List) //return number of lines in txt file to make approproate array (char* Content) size
{
char* Line[50];
int NumLines = 0;
while(!feof(List))
{
fscanf(List,"%s",Line);
NumLines++;
}
rewind(List);
return NumLines;
}
void GetContent(char* Content[][50], int NumLines, FILE* List) //reads each line into array
{
char* Line[50];
int i;
for(i=0; i<NumLines; i++)
{
fscanf(List,"%s",Line);
strcpy(Content[i],Line);
}
}
void SortContent(char* Content[][50], int NumLines) //sorts it alpha.
{
NumLines--;//prevents index out of bounds. I'm using strcmp with index+1
int x, y;
char* Temp[50];
for(x=0; x<NumLines; x++)
{
for(y=0; y<NumLines; y++)
{
if(strcmp(Content[y],Content[y+1])>0)
{
strcpy(Temp,Content[y+1]);
strcpy(Content[y+1],Content[y]);
strcpy(Content[y],Temp);
}
}
}
}
void SaveContent(char* Content[][50], int NumLines, FILE* NewList)
{
int i;
for(i=0; i<NumLines; i++)
{
fprintf(NewList,"%s\n",Content[i]);
}
}
int main()
{
FILE* List = fopen("list.txt","r");
int NumLines = GetNumLines(List);
char* Content[NumLines][50];
GetContent(Content,NumLines,List);
fclose(List);
SortContent(Content,NumLines);
FILE* NewList = fopen("list-edited.txt","w");
SaveContent(Content,NumLines,NewList);
fclose(NewList);
return 0;
}




[Edited by - jmpeer on December 29, 2009 12:44:29 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this