C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 10th, 2013, 11:53 AM
Technovicez Technovicez is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 27 Technovicez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m 21 sec
Reputation Power: 0
Allocation of array size dynamically

can't we allocate arraysize dynamically?
i have two compilers devc++ and ms visualstudio.when i compiled the below program in these two compilers ,dev c++ allowed dynamic allocation of array whereas ms visual studio raised an error
i.e unknown size of the array.
Why it raised this sort of error.
The program is
Code:
#include<stdio.h>
#include<conio.h>

    void change_it(int a)
    {
         a = 555;
}
/*bubble sort*/
void bubblesort(int bubble[],int n)
{
     
    int inc,inc1;
     for(inc=0;inc<n;inc++)
                 {
                    printf("%d",bubble[inc]);
                    }
    for(inc=0;inc<n;inc++)
    {
         for(inc1=(n-1);inc1>inc;inc1--)
         {
            if(bubble[inc1]<bubble[inc1-1])
            {
                 int temp;
                 temp = bubble[inc1];
                 bubble[inc1]=bubble[inc1-1];
                 bubble[inc1-1] = temp;
                 }
         }
}
                 for(inc=0;inc<n;inc++)
                 {
                    printf("%d",bubble[inc]);
                    }                       
    
    
    
}

 
int main(void)
{

    int a =1,n,bubble[n],inc;
/*    void change_it(int);*/
    printf("%d\n",a);
    printf("enter the value of n i.e the size of array\n");
    scanf("%d",&n);
    printf("Enter tihe values in the Array");
    for(inc=0;inc<n;inc++)
    {
            scanf("%d",&bubble[inc]);
            }
    change_it(a);
    bubblesort(bubble,n);
    printf("%d\n",a);
    getch();
    return 0;
    }
    

Reply With Quote
  #2  
Old February 10th, 2013, 02:13 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 18 h 45 m 47 sec
Reputation Power: 1774
> can't we allocate arraysize dynamically?

> int a =1,n,bubble[n],inc;
First of all, the value of n is garbage, so your array length is also garbage. The array length isn't revised automatically whenever you input a new value for n.

Second, variable length arrays such as this were introduced in C99. dev-c++ (which uses gcc, supports C99 (and did so as an extension prior to C99).

Visual studio does not support C99, and so it's a syntax error.

If you want something portable, then do this.
Code:
int *bubble;
...
    printf("enter the value of n i.e the size of array\n");
    scanf("%d",&n);
    bubble = malloc( n * sizeof(*bubble) );
...// do your thing
    free(bubble);
Comments on this post
Technovicez agrees!
__________________
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

Reply With Quote
  #3  
Old February 10th, 2013, 02:14 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 9 sec
Reputation Power: 1974
I don't see where you ever call malloc or calloc to allocate any array dynamically. Just how do you think that you're allocating an array dynamically?

I can't see how MinGW gcc (the compiler that Dev-C++ uses) would have compiled that. Unless it's one of those weird C99 things. MS Visual Studio doesn't support C99, because C99 mostly just tries to bring C up-to-date with C++ and Microsoft is investing in C++ and supports ANSI C (AKA "C89") which is what everybody uses.
Comments on this post
Technovicez agrees!

Reply With Quote
  #4  
Old February 11th, 2013, 12:40 AM
Technovicez Technovicez is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 27 Technovicez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m 21 sec
Reputation Power: 0
Thanks a Ton....
Sir U Specified that
[highlight]
"The array length isn't revised automatically whenever you input a new value for n."
[\highlight]
how to know about this aspect, i mean any resources to know when array is allocated memory whether is it is revised automatically .Not only this in general all datastructures.

Reply With Quote
  #5  
Old February 11th, 2013, 01:49 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 18 h 45 m 47 sec
Reputation Power: 1774
Quote:
Originally Posted by Technovicez
how to know about this aspect, i mean any resources to know when array is allocated memory whether is it is revised automatically .Not only this in general all datastructures.

1. Investigate realloc while you're reading about malloc.

2. Use C++ containers

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Allocation of array size dynamically

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap