Thread: convert int to const int

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    44

    convert int to const int

    I wrote like this but it isn't work

    Code:
    (const *)example;
    example is int. how can I do this??

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    shoud be just

    Code:
    (const int) example;
    since its your not coverting into const integer pointer.

    ssharish

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Const is not a type. You can only cast to a new type. A type can be constant, however, so applying const to the type will make it constant.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    44
    it isnt work! I have simple struct and I want to use like this

    Code:
    void example(int *vle)
    {
          .....
          struct exam{
                 ......
           }x[(const)*vle]
    
    }
    but "x[bla]" you now bla must be const int so I get error..
    sory my english.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Const is not a type.
    Use (const int).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    44
    I used it (const int) but result didnt change...

    I get this error
    1)Constant expression required
    2)Constant variable 'x' must be initialized

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suppose you are trying to create an array of structs with a non-constant expression. This is illegal.
    Consider:
    Code:
    int n = 10;
    int myarray[n]; /* Illegal */
    int myarray[10]; /* Legal */
    It is allowed to create a dynamic array on the stack in C99, but not C89.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    44
    But I must get struct size from another result.. Cant we do this?? :S

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's possible via dynamic memory.
    That is malloc and free. Malloc to allocate a dynamic size of memory and free to free it later when you don't need it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by koyboy View Post
    But I must get struct size from another result.. Cant we do this?? :S
    Yes you could dynamically allocate it use it malloc!!! Given the size and the struct type. You need to be familiar with pointers!

    ssharish.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    44
    thanks for advice but I didnt it in struct.. if I use int so I can do like this:

    Code:
    int * number = (int *) malloc(*nos);
    is it true?? I think yes but I never do it for struct.. How can we do this??

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by koyboy View Post
    thanks for advice but I didnt it in struct.. if I use int so I can do like this:

    Code:
    int * number = (int *) malloc(*nos);
    is it true?? I think yes but I never do it for struct.. How can we do this??
    Should be

    Code:
    int * number = (int *) malloc( *nos * sizeof(int) );
     
    and check for error
     
    if( number == NULL )
       report error;
    You need to specifgy the size to the malloc, otherwise the malloc will assume one byte for every element, which in your case should be int. Since you are trying to allocate integer vector. In the same way if you are allocating float then just replace sizeof(int) with sizof(float) and same applies for the other datatypes.

    ssharish

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    44
    Yes :S but I want it for struct like this

    Code:
    void example(int *vle)
    {
          .....
          struct exam{
                 ......
           }x
    
    }
    I want to use malloc for x struct??
    and add one more question how can we do this for char array??
    Last edited by koyboy; 05-24-2008 at 04:40 PM.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    void example(int *vle)
    {
          .....
          struct exam{
                 int in;
                 float flo;
          };
          
          struct exam *container = malloc( sizeof( struct exam ) * *vle );
          
          // Check for return value of malloc and report error on failure.
    }
    ssharish

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another, more popular way:
    Code:
    struct exam* container = malloc(sizeof(*container) * *vle);
    And never cast the return of malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM