• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1052
  • Last Modified:

Cannot Convert 'Int' to 'Char *'

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.


1 Solution
ss01Author Commented:
Edited text of question
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
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][ ][ ][ ][ ] ....

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

if you let me see your code I will tell you what is going on
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
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.  
// 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);

i get the feeling ss01 is not coming back
ss01Author Commented:
Jmelia and Booth882

Both of you get 50 pts. For the answer.

Thank you.


p.s. Sorry for not answering as I was away
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 :)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now