c++ - Converting int to char* -
i'm creating matrix multiplied one. matrix values arguments (*argv[]).
until created this:
for(int i=0; i<2; i++) {        for(int j=0; j<2; j++) {           sec[i][j]=argv[gv];           gv++;        }     }  for(int i=0; i<2; i++) {    for(int j=0; j<2; j++) {       int = (int)fir[i][j];       int b = (int)sec[i][j];       int r = a*b;       res[i][j]=(char)(r); //the problem here    }    cout<<endl; } the problem code crashes when want show matrix. problem can in marked place.
any ideas how solve simple problem? :)
thanks in advance!
the problem asked about
as didn't show declarations can't tell sure, if crash occurs @ indicated position didn't reserve enough space res. sure declared this?
char res[2][2]; other problems
i'm not sure trying in first loop, if if the identifier argv references parameter passed main in:
int main (int argc, char *argv[]) argv array of pointers strings not array of value of first character contained in strings. you'll filling sec array more or less random values chosen os , abi adheres to store parameters passed function @ location.
essentially setup looks this:
                               +---------> "my_program"  argv -> +--------------+     /              @ anywhere in memory          | argv [0]    ------+          +--------------+          +---->  "paramter argc-2"          | argv [1]    ---------+ /          @ location in memory          +--------------+        \          | ...          |      /   +-----> "paramter 1"          +--------------+     /              @ other location again          | argv[argc-1] |----+          +--------------+ while @ locations performing calculations with.
don't misinterpret << output
while using constructs like
std::cout << argv[1]
will produce same output as:
std::cout << 12
if called program 12 first parameter both seem more or less identical definately not! overloaded << makes them appear same.
nevertheless << toggle different code when called on object of type char* toggled, when run on object of type int.
the difference example
you might want check following snippet see difference in action:
#include <iostream> #include <stdio.h>  int main (int argc, char *argv[]) {   int k;    (k=0; k<argc; ++k)     std::cout << k << ": " << argv[k] << "\n";    (k=0; k<argc; ++k)     printf ("%d: %d\n", k, (int) argv[k]);    return 0; } running generated code ./my_program 12 27 42 produce output following:
0: ./my_program 1: 12 2: 27 3: 42  0: -1079747848 1: -1079747840 2: -1079747837 3: -1079747834  quite difference, results fact when faced value of type char* << "knows" has perform different action when faced value of type int.
by "telling" implementation in second loop right action perform:
  (k=0; k<argc; ++k)     printf ("%d: %s\n", k, argv[k]); you'll same output second loop.
those strange numbers of first output resulted interpreting bitpatterns used represent pointers paramters passed main (bitpatterns) of integers.
typecasting (unsigned) instead , using "%x" instead of "%d" have made more obvious giving following output:
0: bf92bae9 1: bf92baf6 2: bf92baf9 3: bf92bafc as can see "cheated" little bit in picture exaggerating randomness of memory locations.
if count indices of you'll find in example os placed parameters (including termination \0 characters) 1 after other.
Comments
Post a Comment