Cannot Convert 'Int' to 'Char *'

Posted on 1998-06-12
Last Modified: 2011-04-14
How do you get a single word "Now is the time" from a file.
I manage to get a single character into an array. But when I try to strcat the array ie word[0][0] = "N" with word[1][0]= "o" to Temp[80]. I get the above message?? I don't understand both are char arrays. I need to concatenate the word[x][y] to temp ie. temp[0] = "Now"
                       temp[1] = "is"
                       temp[2] = "the"

Any assistance will be greatly appreciated.


Question by:ss01

Author Comment

ID: 1165806
Edited text of question

Accepted Solution

kellyjj earned 100 total points
ID: 1165807
I don't think you meant that you are storing the string in the 'x' element of the array.  With that in mind,  Here is how I would cat it.


a char array set up like  word[50][80]  can be thought of as a array of 50 strings w/ lengths of 80.  

I hope this helps

Expert Comment

ID: 1165808
any array char[50][80] is, as you might know, 2-dimensional.

You can strore a string "Now is the time" in a 1-dimensional array like this:
char String[80]
[N][o][w][ ][i][s][ ][t][h][e][ ][t][i][m][e][\0] ....

The rest of the [50] x [80] array is:
[e][m][p][t][y] ....
[m][ ][ ][ ][ ] ....
[p][ ][ ][ ][ ] ....
[t][ ][ ][ ][ ] ....
[y][ ][ ][ ][ ] ....

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


Expert Comment

ID: 1165809
if you let me see your code I will tell you what is going on

Expert Comment

ID: 1165810
char ** TheCharPointers = new char * [4];

char * TempChar = new char[10];
TempChar[] = "now";
TheCharPointers[0] = TempChar;
TempChar = new char[10];
TempChar[] = "is";
TheCharPointers[1] = TempChar;


now each item in TheCharPointers is an individual word.  
you need to delete all those news when you are done if you do it this way:

for(int i = 0; i < 4; i++)
    delete [] TheCharPointers[i];

delete [] TheCharPointers;

I'm not sure if that's what you asked for, give me feedback

Expert Comment

ID: 1165811
the compiler gets confused sometimes because char and char * variables are kept as integer values and converted with a table.  A char is a one byte binary code, a char * is a hexadecimal address to a char or char array.  This may be what gave you the error message, and even though you say they are both char arrays this still happens sometimes.  You can change an integer into a corresponding char from the table by putting (char) in front of it, for example:

char TheChar = (char) 6;
int Spade = 6;
char TheChar = (char) Spade;

This makes TheChar a spade symbol.

Even though this mixup does happen sometimes, it is usually a signal that something else is wrong, because the ambiguity would not be arising if the variables were being properly used.  If I could see your code I could tell you exactly where your problem is.  

Expert Comment

ID: 1165812
// Reference: Kenigan and Ritchie

Hope this answers your question. Good luck with it..  
The problem is related to syntax. The use of character arrays is one area in 'C' where it is not always obvious to determine the difference between a pointer and a pointer to an address. Hopefully this code example may solve your problem .

Your question centered on 'C' code rahter than CStrings so the asnwer is oriented towards 'C' string, arrays, character pointers and byte addresses.

      char      tempArrayOfChars [80];     // a series of bytes  
      char      word [10][20];                 // 10 words, max of 20 chars per word

      memset (word, (char) NULL, sizeof (word));

      // Notice a string array is being copied to a string array
      strcpy (word [0], "Hello");
      strcpy (word [1], "Word");
      strcpy (word [2], "Append > ");
      strcpy (word [3], "Append > ");

      // Stick a byte into a specified address
      word [0] [0] = (char) 'x';            // Place value at the specified address
      word [0] [0] = (char) 'H';            // Place value at the specified address
      word [1] [1] = (char) 'X';            // Place value at the specified address
      word [1] [1] = (char) 'o';            // Place value at the specified address

      int offsetIntoArray = 9;
      char * pPtr_A = word [2];                                    
      pPtr_A += offsetIntoArray;      // A pointer to a desired location !
      strcpy (pPtr_A, "Here !  ");      // Append this text using a pointer to an address

                                                      //               //  Use the strcat function to do the same thing
      char * pPtr_B = word [3];                                    
      pPtr_B += offsetIntoArray;            // A pointer to a desired location !
      strcat (pPtr_B, "Another !! ");              // Append this text

      char * pSelectedWord = (char *) word [1];      
      strcpy (pSelectedWord, "Replacement");

      strcpy (tempArrayOfChars, pSelectedWord);
      strcat (tempArrayOfChars, pPtr_A);
      strcat (tempArrayOfChars, pPtr_B);


Expert Comment

ID: 1165813
i get the feeling ss01 is not coming back

Author Comment

ID: 1165814
Jmelia and Booth882

Both of you get 50 pts. For the answer.

Thank you.


p.s. Sorry for not answering as I was away

Expert Comment

ID: 8635405
hello guys,
Im designing a little program with MFC and Libnet.The interface has 4 edit boxes (among others): m_source_ip,(long)
              m_src_port (short)

Meanwhile libnet exports a function called libnet_name_resolve and its prototype is:
       unsigned long libnet_name_resolve(unsigned char *,some macro);

This function returns an ip address in Network Byte Order of type unsigned long,wich i saved in src_ip and dest_ip .

Here´s part of the program

//------------------------------------  PROGRAM ENTRY ----------------------------------------------------

unsigned long src_ip,dest_ip; //source and destination ip Network Byte Order
unsigned short src_port,dst_port; //source and destination port

src_ip=libnet_name_resolve((unsigned char *)m_source_ip,LIBNET_RESOLVE);
dest_ip=libnet_name_resolve((unsigned char *)m_dest_ip,LIBNET_RESOLVE);

src_port=(unsigned short)m_src_port;
dest_port=(unsigned short)m_dest_port;

But...The program doesnt work.I used the debugger and a sniffer and the packets go out
with src_port=dest_port=0 and src_ip=dest_ip= like the function isnt resolving nothing....

I tried this way and it worked.....

//------------------------------------  PROGRAM ENTRY ----------------------------------------------------

unsigned char s_ip[]="",dst_ip[]="";
unsigned long src_ip,dest_ip; //source and destination ip Network Byte Order
unsigned short src_port,dst_port; //source and destination port



Using class wizard i have a limited range of data types and using type casting,atoi,htons,htonl,etc...I just cant make it to work....
I made this program in pure c and it worked....Probably its just a stupid mystake but i cant see it....

Hope someone can help me cos im stucked in this sh*t.

PS:Sorry about my English :)


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question