Thread: Variably modified type

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Variably modified type

    Hello to all

    Variably modified types are subject to certain restrictions , just as variable-length arrays are. The most important restriction is that the declaration of a variably modified type must be inside the body of a function or in a function prototype

    Code:
    void f(int m , int n)
    {
    int a[m][n] , (*p)[n];
    
    p=a;
    
    //....
    }
    What does it mean? That we can't declare such a pointer as a global variable?

    Thank you in advance.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    That we can't declare such a pointer as a global variable?
    O_o

    Would you understand the reasons if it were a "VLA" instead of a "VMT"?

    How would you declare the variable `p' at global scope?

    Soma

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by phantomotap View Post
    O_o

    Would you understand the reasons if it were a "VLA" instead of a "VMT"?

    How would you declare the variable `p' at global scope?

    Soma
    I don't know... I just read this and I couldn't bring in my mind some example ... sorry

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Think of a VLA as a clever wrapper around malloc, and work out the contexts in which it is valid to call malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    I think I got it. Variably modified types has the same restrictions as VLA's.

    So you can't declare a VLA outside a function because its dimensions are not known outside from this function.

    Code:
    #include<stdio.h>
    
    int main(void)
    
    {
    int n;
    
    printf("Give n : ");
    scanf("%d" , &n);
    
    int VLA[n]; // inside the function main ....
        
        return 0;
    }
    I think that is a simple reason...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can the some value of array be modified?
    By zcrself in forum C Programming
    Replies: 4
    Last Post: 11-26-2009, 10:22 PM
  2. modified
    By archriku in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 11:34 PM
  3. Date Last Modified
    By magic.mike in forum Windows Programming
    Replies: 5
    Last Post: 09-23-2005, 05:13 AM
  4. Last modified date?
    By Hoxu in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2001, 12:32 PM