-2

I have to make the string receive number1 and number2 values and then print the string as an int, so here i'm converting number1 and number2 from int to string, and trying to put them in str[0] and str[1], but this isn't working... How can i do this? I've been searching on the web but haven't found anything... Can someone help me?

#include <stdlib.h>  
#include <stdio.h>  
#include <sys/time.h>       

struct timeval TempoInicial;   

int main()
{
  int number1, number2;
    gettimeofday(&TempoInicial,NULL);
    number1 = ((TempoInicial.tv_usec / 47) % 1023) + 2;
    number2 = ((TempoInicial.tv_usec / 47) % 1023) + 2; 

  char str[2];
  char n1[1],n2[1];
  int i;

  sprintf(n1, "%d", number1);
  sprintf(n2, "%d", number2);

  str[0]=n1;
  str[1]=n2;


   int conv = atoi(str);

  printf("String: %d %d\n",str[0],str[1]);
}

I get this message:

assignment makes integer from pointer without a cast [enabled by default] str[0]=n1;

teste.c:21:9: warning: assignment makes integer from pointer without a cast [enabled by default] str[1]=n2;

Camila J.
  • 3
  • 3
  • possible duplicate of [How to convert integer to string in C?](http://stackoverflow.com/questions/9655202/how-to-convert-integer-to-string-in-c) – Axalo Apr 29 '15 at 23:03
  • no, it's not the same thing as conversion. I'm already making a conversion in the code – Camila J. Apr 29 '15 at 23:05
  • I think you need space for the null termination (which `sprintf` adds), try making `n1` and `n2` be `n1[2]` and `n2[2]`. – DigitalNinja Apr 29 '15 at 23:06
  • Right... your number strings will be bigger than that even, make `n1` and `n2` larger like 6 or something. – DigitalNinja Apr 29 '15 at 23:11
  • In general, you need 10 digits for 32-bit integers, plus 1 more for the null terminator. In this case you are doing `...% 1023) + 2;` so you need 4 for the digits and 1 more for the null. – Iskar Jarak Apr 29 '15 at 23:18
  • Delete everything from `char str[2];` onwards and put: `char str[100]; snprintf(str, sizeof str, "String: %d %d", number1, number2);` – M.M Apr 29 '15 at 23:34

2 Answers2

0

Your calculation of number1 and number2 will result in a number which can have anything from 1 to 4 digits. 32-bit integers can normally have anything from 1 to 10 digits, but since your formula applies a modulus of 1023, the number will be capped at 1022, or rather 1024, since you add 2 afterward. Thus it can range from 2 to 1024. Hence, your char n1[1] and char n2[1] buffers are not big enough to handle your calls to sprintf(). They must be at least 5 chars long for that, which is necessary for the (up to) 4 digits, plus the terminal NUL added by sprintf().

Furthermore, your attempt to assign the entire n1 and n2 buffers to str[0] and str[1] is incorrect; you cannot assign a char array to a char. I'm also not entirely sure why you want to transfer the values from n1 and n2 to another string buffer.

What you seem to want to do is convert back to int after converting to string. You can do this as follows:

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

struct timeval TempoInicial;

int main() {

    int number1, number2;

    gettimeofday(&TempoInicial,NULL);
    number1 = ((TempoInicial.tv_usec / 47) % 1023) + 2;
    number2 = ((TempoInicial.tv_usec / 47) % 1023) + 2;

    char n1[5],n2[5];

    sprintf(n1, "%d", number1 );
    sprintf(n2, "%d", number2 );

    int i1 = atoi(n1);
    int i2 = atoi(n2);

    printf("%d %d\n", i1, i2 );

}

You seem to have some confusion about what data can be stored in which data types. A char cannot handle a number that can range over 0..1024. A char is an 8-bit value, thus it can only hold 2^8 == 256 possible values. Thus it is incorrect to assign these numbers to str[0] or str[1].

If you want a string representation of the numbers, you already have it in n1 and n2, courtesy of sprintf(). If you want an integral representation of the numbers, you already had it in number1 and number2, and you can get it again from the string representations courtesy of atoi(), as I show in my code. If you want to print out the string values, you can say something like printf("%s %s\n", n1, n2 );. If you want to print the integral values, you can say something like printf("%d %d\n", i1, i2 );, as I show in my code. Those are all the actions that I believe are relevant to what you're trying to do. Let me know if you have any more questions about anything.


Ah, I think I see what you're after. You can combine n1 and n2 into the same string with sprintf(). You just have to be careful to use a sufficiently large buffer, which will be 4 digits + 1 space + 4 digits + 1 NUL == 10 as the upper limit or "worst case" scenario, if you will. (The "best case" scenario is 1 digit + 1 space + 1 digit + 1 NUL == 4.) Thus we have:

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

struct timeval TempoInicial;

int main() {

    int number1, number2;

    gettimeofday(&TempoInicial,NULL);
    number1 = ((TempoInicial.tv_usec / 47) % 1023) + 2;
    number2 = ((TempoInicial.tv_usec / 47) % 1023) + 2;

    char n1[5],n2[5];
    sprintf(n1, "%d", number1 );
    sprintf(n2, "%d", number2 );

    char str[10];
    sprintf(str, "%s %s", n1, n2 );

    printf("%s\n", str );

}
bgoldst
  • 34,190
  • 6
  • 38
  • 64
  • Thank you a lot! I understand what you said, and it worked! But what i really need is to put the values of n1 and n2 inside of the string str (it's the activity my professor asked us). But i still don't know how to do that. N1 and n2 have to be passed to str, and i have to print str. Can i still do that with the tools you told me? – Camila J. Apr 29 '15 at 23:36
  • Thank you so much!! I didn't know that sprintf could concatenate too – Camila J. Apr 29 '15 at 23:46
  • You're very welcome :) Don't forget to upvote all answers that helped you and accept the answer that helped you most ;) – bgoldst Apr 29 '15 at 23:46
0

The C strings are arrays of chars terminated by a zero. They should be big enough to contain all the chars that represent the string you want plus one byte to insert the value 0 (the string terminator). The n1 and n2 char arrays in your code might have a dimension of 5 byte. The char str[] in your code doesn't nothing useful, then I've deleted it.

Try this:

int main()
{
    int number1, number2,conv;
    char n1[11],n2[11];

    gettimeofday(&TempoInicial,NULL);
    number1 = ((TempoInicial.tv_usec / 47) % 1023) + 2;
    number2 = ((TempoInicial.tv_usec / 47) % 1023) + 4; /* +4 to be different from n1!*/

    sprintf(n1, "%d", number1);
    sprintf(n2, "%d", number2);

    conv = atoi(n1);
    printf("String: %s %d\n",n1,conv);
    conv = atoi(n2);
    printf("String: %s %d\n",n2,conv);

    return 0;
}

If you need to use str as a new string different from n1 and/or n2 you have to declare it as:

char str[11];

then, to copy the value of n1 into str, you have to use strcpy:

strcpy(str,n1);

If you want to have pointers to the strings n1 and n2 so that changing the contents of str[0] you change n1 and changing the contents of str[1] you change n2, you have to declare:

char * str[2];

and then you have to assign the value of n1 and n2 to str[0] and str[1]:

str[0]=n1;
str[1]=n2;
Sir Jo Black
  • 2,024
  • 2
  • 15
  • 22