I usually declare something like that as:
Code:
char* terry = "Hello";
That is, if I don't want to modify the buffer. That's the only thing I can think of that would cause a problem in that code.
ephan wrote:
I was reading a CPlusPlus.com tutorial's on C++ and learnt a lot of stuff (still haven't finished it), and I have a question:


Code:

#include <iostream>
using namespace std;

int main ()
{
  char terry[] = "Hello";
 
  if (*(terry+5) == "\0") {
    cout << "true";
  }
 
  return 0;
}


The thing is, terry is an address. terry+5 is an address to the "\0", the end of the character.

So it's like:

H E L L O \0
^
terry

When compiling, G++ says "work.cpp:8:21: error: ISO C++ forbids comparison between pointer and integer"

The thing is, *(terry+5) is not a pointer, it's "\0", I think.


That should be single quotes, ie *(terry+5) = '\0'

'' == char
"" == char*
Oh thanks a lot, it makes sense Wink And it obviously returned "true" because the char '\0' marks the end of the string.


Code:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]);

int main(int argc, char *argv[]) {
  char n_input[99];
  int n;
  fgets(n_input, sizeof n_input, stdin);
  sscanf(n_input, "%d", &n); //get the number of times out of n_input to n

  char ** all_input[n];
 
  char * two_numbers[2];
  int i;
  char input[100];
  char * saveptr1;
  for(i=0; i<n; i++) {
    fgets(input, sizeof input, stdin);
   
    strtok_r(input, " ", &saveptr1);
   
    two_numbers[0] = input;
    two_numbers[1] = saveptr1;
   
    all_input[i] = two_numbers;
  }


My goal is to get input like:

Quote:

2
12 19
21 30


And then have an array like:

Quote:

{ {"12", "19"}, {"21", "30"} }


However, what I get on the array is:

Quote:

{ {"21", "30"}, {"21", "30} }


I don't quite see why this is happening, because:


Code:

fgets(input, sizeof input, stdin);
strtok_r(input, " ", &saveptr1);   
two_numbers[0] = input;
two_numbers[1] = saveptr1;
all_input[i] = two_numbers;


I'm adding "two_numbers" in "all_input[i]", so the elements before [i] shouldn't change to "two_numbers" too.

Unless if this is a memory problem, and I need to allocate memory or something.
Check out the post I made on the previous page that answers exactly that.

Code:
  int n_buildings;
  cin >> n_buildings;
  string all_heigths;
  getline(cin, all_heigths);


Why does this only take input once? I have a cin and a getline, it's supposed to take 2 inputs.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10
» View previous topic :: View next topic  
Page 10 of 10
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement