char pointer vs int pointer

This is a discussion on char pointer vs int pointer within the C++ Programming forums, part of the General Programming Boards category; Why is it legal to create a char pointer and assign it a value and not to do the same ...

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    char pointer vs int pointer

    Why is it legal to create a char pointer and assign it a value and not to do the same thing with a int pointer.

    I know that when you declare a pointer you must tell it were to point, therefore the second line should give a compiler error but why doesn´t it generate an error on the first line???

    Code:
    char *name="Smith";
    int *number = 25;//Generates an error
    Thx in advance

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The first line tells the compiler to create a string literal somewhere in memory and to point to the first cell of that array. The synatx for assigning a pointer to an array makes it clear:



    int *pArray;

    int array[10];

    pArray = array;

    ...is the same as:

    pArray = &array[0];

    However, were you to do:

    pArray = 5; //...illegal..assignment of pointer to integer...

    ...same as:

    pArray = someNum; //...error..assignment of pointer to integer...


    Yet:

    pArray = &someNum; //...fine

    I hope I haven't confused you!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    k, get the point. You could say that an array and a pointer is "almost" the same thing, or do they just behave simelair?

    Code:
    char *name = "Smith";
    //char name[] = "Smith";
    Thx in advance

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    char *name = "Smith";

    I think this is an old notation and you're not supposed to use this.
    It is replaced by char name[] = "Smith"; but the old notation is still supported.

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Thx Monster, everything is clear now!!!!

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The only difference that I know is that the old notation is a const char * and cannot be changed:
    Code:
    int main()
    {
      char *s1 = "Smith";
      char s2[] = "Smith";
    
      s1[0] = 'A'; /* not valid */
      s2[0] = 'A'; /* valid */
      return 0;
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You could say that an array and a pointer is "almost" the same thing, or do they just behave simelair?
    Almost, but not exactly. For instance, with an arrays name, you cannot increment it:

    char string[100];
    strcpy(string, "Some String...");

    string++; //...absolutely illegal...

    char *s = string;

    while(*s != 0)
    {
    s++; //...legal
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  5. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21