c++ array

Discussion in 'Programming' started by dracula51, May 5, 2012.

  1. #1
    i was trying to declare an array first then push value like these

    #include<iostream>
    using namespace std;
    
    int main()
    {
        unsigned var[];
        
        var[0] = 56;
        var[1] = 569;
        var[2] = 2;
        
    }
    Code (markup):
    why its not working?? :(
     
    dracula51, May 5, 2012 IP
  2. Joker-jar

    Joker-jar Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You need to specify array length:unsigned int var[3];or allocate memory for it runtime
     
    Joker-jar, May 6, 2012 IP
  3. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tnx
    but Array length is unknown
    i mean in this example i used only 3 but in real script it cud be more or less..
    allocate memory for it runtime
    whts tht? how can u do tht? any example?
    sorry im novice at c++
     
    dracula51, May 6, 2012 IP
  4. Joker-jar

    Joker-jar Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Ok :)

    int main()
    {
      int *var; //pointer to int
    
      int array_length = 3; //array length. this variable can be calculated
    
      var = new int(array_length); //allocate memory to array according array_length
    
      var[0] = 56;
      var[1] = 569;
      var[2] = 2;
    
    }
    Code (markup):
     
    Joker-jar, May 6, 2012 IP
  5. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    tnx again mate but still error wht im trying to do
    here is my code

    
    #include <iostream>
    
    using namespace std ;
    
    int main()
    {
        int first;
        int second;
        int n=1;
        
        //maybe i hav to declare array variable here
        int allFirst;
        int allSeconds;
        
        do
        {
              cout << "enter first number: ";
              cin >> first;
              allFirst[n] = first;
              
              cout << "enter second number: ";
              cin >> second;
              allSecond[n] = second;
    
              n++;
                  
        }while(first > second);
    }
    
    Code (markup):
    so, its unknown how many loops...so whts arrays lengths. its varies
    i hav to store all the inputs using Array. cant use another method :(
    & although in example i used only int, i might have to add char, unsigned etc also

    plz help :((
     
    dracula51, May 7, 2012 IP
  6. bhimesh

    bhimesh Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ushould use dynamic memory allocation. small example in c
    int *ptr,len;

    printf("Enter the size of array u want\n");
    scanf("%d",&len);

    ptr = malloc(len*sizeof(int));

    then u can access ptr i starting from 0 to len .... This is simple c program buddy
     
    bhimesh, May 7, 2012 IP
  7. joerudy1988

    joerudy1988 Peon

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You need to specify the length first.
     
    joerudy1988, May 7, 2012 IP
  8. ipoh

    ipoh Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Please use this one

    unsigned var[] = {1, 2, 3};
     
    ipoh, May 14, 2012 IP
  9. alamlinks

    alamlinks Well-Known Member

    Messages:
    992
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    140
    #9
    void mail()
    {
    int * var;
    var= new var[3];
    var[0]=343;
    var[1]=32;
    varp2]=43;

    }
     
    alamlinks, May 14, 2012 IP