1

Redoing my previous question since I didn't provide enough detail.

I have a char pointer array, char* token[100]. Let's say I have a double-digit number, like 33.

How do I assign this int into an index in the token array, so that when I print out that token it will give me 33 and not some sort of ASCII value?

char* token[100];
int num = 33;
//How do I assign num into a specific token index, like for example:
token[1] = num;
//When I print out that token index, I want 33 to be printed out
cout << token[1] << endl; // I want to have 33 be the result. Right now I have '!' as an output
16
  • 1
    Just cast back to an integer before printing? Commented Feb 17, 2020 at 22:53
  • First you need to understand that a char* is a pointer to a location in memory. You have set it to the value 33, which almost certainly isn't within your program's address space. Then you are trying to read a string which you incorrectly assume is stored in that memory location and write it to standard out. Commented Feb 17, 2020 at 22:53
  • Do you mean you want to print out 33 and not 42? Commented Feb 17, 2020 at 22:55
  • @John Yes, sorry, that's a typo Commented Feb 17, 2020 at 22:56
  • 1
    Do you know the difference between a char *[] and a char []? Do you know the difference between a char and an int? Do you know what an array is? We're asking these questions so we can direct you to the best resources. We need to gauge your level of understanding. Commented Feb 17, 2020 at 22:59

2 Answers 2

1

It seems you mean something like the following

#include <iostream>
#include <string>
#include <cstring>

int main() 
{
    char * token[100] = {};
    int num = 33;

    std::string s= std::to_string( num );

    token[1] = new char[s.size() + 1];

    std::strcpy( token[1], s.c_str() );

    std::cout << "token[1] = " << token[1] << '\n';

    delete [] token[1];

    return 0;
}

The program output is

token[1] = 33

If you are not allowed to use C++ containers and functions then the program can look the following way

#include <iostream>
#include <cstdio>
#include <cstring>

int main() 
{
    char * token[100] = {};
    int num = 33;

    char buffer[12];

    std::sprintf( buffer, "%d", num );

    token[1] = new char[std::strlen( buffer ) + 1];

    std::strcpy( token[1], buffer );

    std::cout << "token[1] = " << token[1] << '\n';

    delete [] token[1];

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, thank you! That is what I meant. However, my teacher doesn't allow me to use the std:: commands, so is there a way to do this that doesn't use those?
To handle all 32-bit int, char buffer[12]; makes more sense.
0

I'm convinced, from comments, that you want an array of integer types. If we get further clarification about why this needs to be a char array, I'll update my answer, but from all available information it seems like you really want an integer-type array.

#include <iostream>

int main(int argc, char** argv)
{
    int token[100] = {};
    int num = 33;

    token[1] = num;

    std::cout << token[1] << std::endl;

    return 0;
}

5 Comments

This needs to be a char array because I am reading in an equation like this (3 + 34 x 7) and I need to basically have each number in its own array for example: token[3][+][34][x][7].
If your equation was 3 + 256 what tokens would be stored in the char array? A char is usually only 8 bits, so that 256 will overflow if you try to store it as a single char.
ok maybe I don't completely understand chars but I thought that 256 would be treated as a string and would just be token[256]
@madaniloff What is the input and output of your program? Do you need to solve the typed in equation? Because the equation 3 + 34 x 7 would be stored as the character array {3, 43, 34, 120, 7}. After storing it in an array, how can you tell which of those chars are operators and which are numbers?
Basically I am taking user input from a char, so, for example, input would be input[3][+][34][x][7]. If the number is a single digit or operator I am just assigning it to the token array like this token[i] = input[i].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.