tags:

views:

66

answers:

3

Hi All,

I am new to c++ programming I have to call a function with following arguments. int Start (int argc, char **argv).

When I try to call the above fuction with the code below I get run time exceptions. Can some one help me out in resolving the above problem.

char * filename=NULL;
char **Argument1=NULL;
int Argument=0;
int j = 0;
int k = 0;
int i=0;

int Arg()
{
filename = "Globuss -dc bird.jpg\0";

for(i=0;filename[i]!=NULL;i++)
 {
   if ((const char *)filename[i]!=" ")
    {
   Argument1[j][k++] = NULL; // Here I get An unhandled 
                             // exception of type 
                             //'System.NullReferenceException' 
                             // occurred
       j++;
       k=0; 
    }

   else
    {
       (const char )Argument1[j][k] = filename [j]; // Here I also i get exception
        k++;
        Argument++;
    }
 }

Argument ++;
return 0;
}

Start (Argument,Argument1);
A: 

You appear to have no allocated any memory to you arrays, you just have a NULL pointer

char * filename=NULL;
char **Argument1=NULL;
int Argument=0;
int j = 0;
int k = 0;
int i=0;

int Arg()
{
filename = "Globuss -dc bird.jpg\0";

//I dont' know why you have 2D here, you are going to need to allocate
//sizes for both parts of the 2D array
**Argument1 = new char *[TotalFileNames];
for(int x = 0; x < TotalFileNames; x++)
    Argument1[x] = new char[SIZE_OF_WHAT_YOU_NEED];

for(i=0;filename[i]!=NULL;i++)
 {
   if ((const char *)filename[i]!=" ")
{
   Argument1[j][k++] = NULL; // Here I get An unhandled 
                         // exception of type 
                         //'System.NullReferenceException' 
                         // occurred
       j++;
       k=0; 
    }

   else
    {
       (const char )Argument1[j][k] = filename [j]; // Here I also i get exception
        k++;
        Argument++;
    }
   }

  Argument ++;
  return 0;
  }
Craig
This is assuming you want a 2D array, but i don't think you need it. You want a 2D array if you wish to store multiple file names such as.[0]["File One"][1]["File Two"][2]["File Three"]but i think you want a 1D array[F][I][L][E][ ][O][N][E]
Craig
Hi Craig,Thanks for your valuable suggestions. I have tried your suggestions but the same exception is encountered. My main motive is to call the Start(Argument, **Argument); fuction.
Ravi shankar
Do you know how the two dimensions arrays work? I don't mean to sound disrespectful but what it appears you wants from the code is [0][f][1][i][2][l][3][e], as a 2 dimensional array, you are allocating two much room. if you want it so it is [0][filename1][1][filename2], just remove the j++ from the loop and increment it everytime you want to add another filename
Craig
A: 

Two things:

char **Argument1=NULL;

This is pointer to pointer, You need to allocate it with some space in memory.

*Argument1 = new char[10];

for(i=0, i<10; ++i) Argument[i] = new char();

Don't forget to delete in the same style.

bua
A: 

The first thing you have to do is to find the number of the strings you will have. Thats easy done with something like:

int len = strlen(filename);
int numwords = 1;

for(i = 0; i < len; i++) {
    if(filename[i] == ' ') {
        numwords++;
        // eating up all spaces to not count following ' '
        // dont checking if i exceeds len, because it will auto-stop at '\0'
        while(filename[i] == ' ') i++; 
    }
}

In the above code i assume there will be at least one word in the filename (i.e. it wont be an empty string). Now you can allocate memory for Argument1.

Argument1 = new char *[numwords];

After that you have two options:

  1. use strtok (http://www.cplusplus.com/reference/clibrary/cstring/strtok/)
  2. implement your function to split a string

That can be done like this:

int i,cur,last;
for(i = last = cur = 0; cur < len; cur++) {
    while(filename[last] == ' ') { // last should never be ' '
        last++;
    }

    if(filename[cur] == ' ') {
        if(last < cur) {
            Argument1[i] = new char[cur-last+1]; // +1 for string termination '\0'
            strncpy(Argument1[i], &filename[last], cur-last);
            last = cur;
        }
    }
}

The above code is not optimized, i just tried to make it as easy as possible to understand. I also did not test it, but it should work. Assumptions i made:

  1. string is null terminated
  2. there is at least 1 word in the string.

Also whenever im referring to a string, i mean a char array :P

Some mistakes i noticed in your code:

  1. in c/c++ " " is a pointer to a const char array which contains a space. If you compare it with another " " you will compare the pointers to them. They may (and probably will) be different. Use strcmp (http://www.cplusplus.com/reference/clibrary/cstring/strcmp/) for that.
  2. You should learn how to allocate dynamically memory. In c you can do it with malloc, in c++ with malloc and new (better use new instead of malloc).

Hope i helped!

PS if there is an error in my code tell me and ill fix it.

George B.