Thread: Error on Function with Array

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    Error on Function with Array

    Hi to All
    I am a new user of C++ an d I need help.
    this is part of my program:
    Code:
    #define ADIM 5
    ...
     
    char *otherString(char *Strings);
     
    int main()
    {
    char firstString [5];
    char secondString[5];
    .
    .
    .
    // for example the firstString is 'ABCD' + '\0'
    // I need to create a secondString with a function as this
    secondString=otherString(firstString);
    }
     
    // I have create this function
    char *otherString(char *Strings)
    {
    char *tmpArray;
    /* Allotion of Array */
    tmpArray = new char*[]; 
    for(int i=0; i<ADIM; i++) 
          tmpArray[i] = new char[ADIM];
     
          tmpArray[0] = Strings[2];
          tmpArray[1] = Strings[0];
          tmpArray[2] = Strings[3];
          tmpArray[3] = Strings[1];
          tmpArray[4] = '\0';
    return tmpArray;
    }
    During the compilation, VS return to me this errors.
    error C2440: '=': impossible to convert from 'char *' to 'char [5]' and other
    Please, be patient with me. Can You help me to correct this code ?

    Thank You and Best Regards

  2. #2
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    can you say at which line is this occuring ?
    When you are declaring first and second string you shouldn't say [5], just *.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    26
    Quote Originally Posted by ElastoManiac
    can you say at which line is this occuring ?
    Thank You for reply.
    This is the complete code:
    Code:
    #include <iostream>
    using namespace std; 
    #define DIM 2
    #define ADIM 5
    char firstString[5];
    char secondString[5];
    void fillMatrix (char [][DIM], int);
    void createString (char [] [DIM]);
    char *otherString(char *Strings);
    int main()
    {
    char Matrix2x2[ DIM][DIM];
    fillMatrix(Matrix2x2, DIM);
    cout << "Create a String of 4 char plus terminator from Matrix 2x 2;
    createString(Matrix2x2);
    return 0; 
    }
     
    void fillMatrix(char mat[][DIM], int dim)
    {
    cout << "Fill Matrix 2x2" << endl;
    for (int i=0; i<DIM; i++)
    for (int j=0; j<DIM; j++)
    {
    cin >> mat[i][j];
    }
    return;
    }
     
    void createString (char mat[] [DIM])
    {
    int k;
    k=0;
    for (int i=0; i<DIM; i++)
    for (int j=0; j<DIM; j++)
    {
    firstString[k] = mat [i] [j];
    k++;
    }
    cout << endl;
    for (int k=0; k<4; k++)
        cout << firstString[k];
    firstString[4] = '\0';
    cout << endl;
    cout << firstString;
     
    // Create secondString  Here is the error
    secondString=otherString(firstString);
    }
     
     
    char *otherString(char *Strings)
    {
    char *tmpArray;
    /* Allocazione dinamica dell' Array */
    tmpArray = new char*[]; 
    for(int i=0; i<ADIM; i++) 
    tmpArray[i] = new char[ADIM];
    tmpArray[0] = Strings[2];
    tmpArray[1] = Strings[0];
    tmpArray[2] = Strings[3];
    tmpArray[3] = Strings[1];
    tmpArray[4] = '\0';
    return tmpArray;
    }
    This is the complete code with the line of error marked.
    Please can You help ?

    Best Regards
    Last edited by Ken Fitlike; 11-21-2006 at 06:34 AM. Reason: please don't use midget fonts within [code][/code] blocks

  4. #4
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    First of all, if you want to use strings i reccomend the string class.
    first and second strings are declared as arrays of 5 chars. while your function return char*.
    change the first and seconds string into char* xxx;

    You should create your own string class, or use the one from Standard C++. It isn't good to use char*
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    const size_t ADIM = 5;
    
    char *otherString(const char *Strings)
    {
    	char *tmpArray;
    	tmpArray = new char[ADIM]; 
    	tmpArray[0] = Strings[2];
    	tmpArray[1] = Strings[0];
    	tmpArray[2] = Strings[3];
    	tmpArray[3] = Strings[1];
    	tmpArray[4] = '\0';
    	return tmpArray;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You also need to forget about using that crappy code colouring tool you're using.

    > usingnamespace std;
    It seems to modify your code into something which won't even compile. How many other mistakes does it introduce?

    Regular [code][/code] tags are all that is needed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    26
    Hi,
    I have make your suggest modification, but the program don't run.

    Have You try with my code ?

    Can You to post the code modified, in order to verify my errors ?

    Thank You

    gaetano

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest code.
    Can you be more specific than "doesn't work?"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. Replies: 3
    Last Post: 03-23-2002, 04:20 PM