4 Replies - 3990 Views - Last Post: 25 July 2012 - 04:58 PM Rate Topic: -----

#1 JohnNash   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-May 12

Initializing two dimensional array using strtok function

Posted 25 July 2012 - 11:08 AM

Hello Everyone!

Below, I have pasted my C++ code where I am trying to initialize a two dimensional character arrays using the strtok function of the string.h . However, when I run the code, I get the following error: incompatible types in assignment of 'char*' to char[30]. In the code, I have also marked the line where I get this error.


#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<string.h>

using namespace std;

int main()
{

    char str[30]="Adam is really cool.";

    char (*ptr)[30]; 

    int i=0;
    char *str1= strtok(str," .");

    while(str1!=NULL)
    {
        ptr[i]=str1; // [b]This where the compiler gives me an error.[/b]
        ptr++;
        str1 = strtok(NULL," .");
        i++;
    }
  int j=0;
 
 while(j < 4)
{
    cout<<ptr[j]<<"\n";
    j++;
}

    getchar();
}


Please help me fix the above problem.

Thank You

:code:

This post has been edited by jimblumberg: 25 July 2012 - 11:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Initializing two dimensional array using strtok function

#2 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Initializing two dimensional array using strtok function

Posted 25 July 2012 - 11:14 AM

What are you actually trying to accomplish?

Since this is a C++ program, why are you using strtok() instead of using C++ std::strings and stringstreams?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 JohnNash   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-May 12

Re: Initializing two dimensional array using strtok function

Posted 25 July 2012 - 11:19 AM

View Postjimblumberg, on 25 July 2012 - 11:14 AM, said:

What are you actually trying to accomplish?

Since this is a C++ program, why are you using strtok() instead of using C++ std::strings and stringstreams?

Jim

I am trying to initialize a two dimensional array using the strtok() function. Yes, it is indeed c++ code, but I curious why strtok() function is not allowing me to initialize a twoD array.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Initializing two dimensional array using strtok function

Posted 25 July 2012 - 11:25 AM

The purpose of strtok() is not to initialize a two dimensional array, its purpose is to:

Quote

Split string into tokens
A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.


So I ask again what exactly are you trying to accomplish?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Initializing two dimensional array using strtok function

Posted 25 July 2012 - 04:58 PM

I'm not sure what this does :

15	    char (*ptr)[30]; 




If you want a one dimensional array of pointers to char :-

   char *ptr[30]; 




The name of the array is a constant and cannot be used like so :-

23	        ptr++;






If you want a two dimensional array of char :-

   char str_array[10][30]; 




You would need to copy the strings probably using strcpy().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1