Link to home
Start Free TrialLog in
Avatar of ss01
ss01

asked on

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"
                       etc...

Any assistance will be greatly appreciated.

Thanks


Avatar of ss01
ss01

ASKER

Edited text of question
ASKER CERTIFIED SOLUTION
Avatar of kellyjj
kellyjj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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][ ][ ][ ][ ] ....
.......


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;

etc...

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;
or
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
Avatar of ss01

ASKER

Jmelia and Booth882

Both of you get 50 pts. For the answer.

Thank you.

ss01

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_dest_ip,(long)
              m_src_port (short)
              m_dest_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 ----------------------------------------------------
blah,blah,blah....

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=255.255.255.255.Seems like the function isnt resolving nothing....

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

//------------------------------------  PROGRAM ENTRY ----------------------------------------------------
blah,blah,blah....

unsigned char s_ip[]="192.168.0.1",dst_ip[]="192.168.0.2";
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(s_ip,LIBNET_RESOLVE);
dest_ip=libnet_name_resolve(d_ip,LIBNET_RESOLVE);

src_port=1025;
dest_port=80;

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 :)