[Question] Array Parameters

Started by
4 comments, last by jmpeer 14 years, 3 months ago
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="Meaningless Content";
        printf("%s\n",MyArray);           //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);          //This is where it doesn't work.
    }
    return 0;
}
Advertisement
You want to pass by reference instead of by pointer.
//void MyFunction(char* MyArray)void MyFunction(char &MyArray)
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.
It would also be totally wrong, because this:

void MyFunction(char &MyArray)

gets an argument which is a reference to a single byte.
my game development site:http://sites.google.com/site/billgamedevelopment/
char v1; // A characterchar* v2; // A pointer to characterchar v3[50] // Array of characterchar v4[10][50] // Two-dimesional array of characterchar* v5[10][50] // Two-dimesional array of pointer to character


You can not assign a string to a raw character array like this
v4 = "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, "Meaningless Content");
The strcpy function will typically loop throught the string copying one character at a time
[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,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);    }}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]

This topic is closed to new replies.

Advertisement