Thread: pointer-convert-problem

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    pointer-convert-problem

    Code:
    #include <iostream.h>
    #include <string.h>
    char* displaystring(ostream& out,istream& in,char string2display[30])
    {
    	string2display=strcat(string2display,"-");
        string2display=strcat(string2display,"is your name");
        char* pointer= new char;
        pointer=&string2display;
    	return pointer;
    }
    this is just a practise-module but it doesnt work
    my compiler says:error C2440: '=' : cannot convert from 'char * []' to 'char'

    er.. help me please
    Last edited by Ivan!; 01-04-2003 at 09:15 AM.

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    just to fix the immediate problem:

    Code:
    pointer = &string2display;
    should be replaced with

    Code:
    pointer = string2display;
    also,

    Code:
    char* pointer = new char;
    pointer = string2display;
    that's a memory leak. you allocate space for a character, then throw it away. just use:

    Code:
    char *pointer = string2display;
    .sect signature

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    if I leave the function the pointer wil be thrown away so I need 2 use the heap, but that isnt the problem for the compiler says:
    '=' : cannot convert from 'char * []' to 'char *'

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Yeah, there are quite a few problems with the function -- more logical than syntactic. For one -- why are you passing references to ostream and istream objects when you aren't using them, it's silly. Also, do you want to return a copy of display string or what? Do you want the parameter to be limitted to a 30 char string? If so, you're declaring it wrong.

    If what you want to do is return a duplicate, then do this:

    Code:
    #include <cstring>
    
    char* displaystring(char (&string2display)[30])
    {
        strcat(string2display,"-is your name");
        char* pointer= new char[30];
        strcpy( pointer, string2display );
        return pointer;
    }
    Just remember to delete the array returned after the function call.

    Again, you most-likely want to completely rethink the function.

    EDIT: also, there are problems with concatenating as you are doing because you never check to be sure the array is large enough.

    EDIT2: post a thorough explanation of exactly what it is that you'd like to do. Until then, we're just guessing as to what you want.
    Last edited by Polymorphic OOP; 01-04-2003 at 09:22 AM.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    Thanx, my function was indeed not so well and your code works good

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-08-2009, 11:47 AM
  2. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  4. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM