Using a function with a char *array

Hey all -- this problem is pretty involved, but basically I have been killing myself simply trying to get started. I need to create a function that splits up the string and prints out a formatted result. If someone can just help me get this syntax correct I believe I can figure out the rest.

The fact that this is a pointer array is throwing me off. In my function, all I am trying to do first is to put a null terminator wherever a ',' exists in the original array. No matter what I have done, it continually has crashed upon launch. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<cstring>
using namespace std;

void printInfo(char*, char);

int main()
{
   char symbolDelimiter = ',';

   char *array1[] = { "joe smith, joe, aol.com", "sue jones, sue, gmail.com", // why isnt this just an array? 
      "bob adams, bob.adams, yahoo.com", "pam oliver, pamo, att.net",
      "ron johnson, jron, sbcglobal.net", "jenny jorgan, jj1, msn.com" };

   printInfo(*array1, symbolDelimiter);
  
   return 0;
}

void printInfo(char *array1, char ch)
{

   while (*array1 != '\0')
   {
      if (*array1 == ch)
      {
         *array1 = '\0';
      }

      array1++;

   }

   cout << array1; // I know this will not give me a full output of what I need, but 
                           // it is simply more of a stub for me to check.

}








Last edited on
What are you required to accomplish?

I see that you have a array of c-strings and you need to do some operations on them. It is sensible to process one string at a time in a loop instead of trying to do it all at once.

Create function which works just with char* string and then just call it in a loop to process array of string.

Now for actual problem:
all I am trying to do first is to put a null terminator wherever a ',' exists in the original array.
Are you really required to replicate usually undesireable behavior of strtok? If so, my condolences.
Your proble is that sting literals are immutable and cannot be changed. In fact your code is invalid, it should be const char *array1[] . It won't compile for me in strict standard mode and after relaxing I still get 6 warnings
\Test\main.cpp|13|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|


You either need to copy string literals in mutable memory or use another approach.
Your array1 (in main) is an array of pointers. Your lines 11-13 is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char * t1 = "joe smith, joe, aol.com";
char * t2 = "sue jones, sue, gmail.com";
char * t3 = "bob adams, bob.adams, yahoo.com";
char * t4 = "pam oliver, pamo, att.net";
char * t5 = "ron johnson, jron, sbcglobal.net";
char * t6 = "jenny jorgan, jj1, msn.com";

char * array1[6];
array1[0] =  t1;
array1[1] =  t2;
array1[2] =  t3;
array1[3] =  t4;
array1[4] =  t5;
array1[5] =  t6;

The real problem is that you create non-const pointers to constant string literals and then attempt to write via one such pointer.

Writes to constant literal memory is undefined behaviour.
Last edited on
This get your code working, but setting elements of the input array to '\o' is not a good idea as it destroys the original data. There are uch better approaches!

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<cstring>
using namespace std;

void printInfo(char*, char);

int main()
{
   char symbolDelimiter = ',';

   // now an array of char buffers (so are writable)
   char array1[][40] = { "joe smith, joe, aol.com", "sue jones, sue, gmail.com",
      "bob adams, bob.adams, yahoo.com", "pam oliver, pamo, att.net",
      "ron johnson, jron, sbcglobal.net", "jenny jorgan, jj1, msn.com" };

   printInfo(*array1, symbolDelimiter); // just the first array element

   return 0;
}

void printInfo(char *array1, char ch)
{
   char *temp = array1; // ADDED
   while (*array1 != '\0')
   {
      if (*array1 == ch)
      {
         *array1 = '\0';
         cout << "array1 = " << temp << "\n"; // ADDED
         temp = (array1 + 1); // ADDED
      }

      array1++;
   }

   if('\0' != *temp)                        // ADDED
       cout << "array1 = " << temp << "\n"; // ADDED

   cout << "array1 = " << array1 << "\n";
   // I know this will not give me a full output of what I need, but 
   // it is simply more of a stub for me to check.
   //
   // By the time you get here array1 is pointing at the original '\0'
   // terminator of the input string
}
Last edited on
Got it working. Thanks a lot for all of your input. This place is an invaluable resource!
Topic archived. No new replies allowed.