Problem with Dynamic Array

This is a discussion on Problem with Dynamic Array within the C Programming forums, part of the General Programming Boards category; Hi All.. I cannot find the mistake in this code. Code: #include <stdio.h> #include <stdlib.h> void create_vect (int** v) { ...

  1. #1
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18

    Problem with Dynamic Array

    Hi All.. I cannot find the mistake in this code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void create_vect (int** v) {
       *v = (int *) malloc(2 * sizeof(int));
       *v[0] = *v[1] = 85;
    }
    
    
    int main()
    {
        int* v;
    	create_vect(&v);
    	printf("%d %d\n", v[0], v[1]); //it prints "85 0". Why? It should be "85 85"
    
    	return 0;
    }
    PS: I don't want to change my function to "int create_vect (void)". I'd like to know why it doesn't work in THIS way

    Thank you in advance !!!!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Because of operator precedence, you need to change this:
    Code:
    *v[0] = *v[1] = 85;
    to this:
    Code:
    (*v)[0] = (*v)[1] = 85;
    [] has higher precedence than *, so an expression like *v[0] evaluates to *(v[0]). But if you think about it, you need (*v)[0]. To get that, you add the parentheses.

    Here's an operator precedence table in case you're interested (it's kind of technical): http://cppreference.com/operator_precedence.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    Oh my god !!!

    Really Fast Reply

    I'm so stupid.. I didn't know about that precedence

    Anyway, Thank you very much Now it woks !

    I've del.icio.used your website

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes, you tend to get very fast replies on this board, depending on when you post the question. Sometimes you could get three people answering your question within as many minutes.

    I don't update mny website very often . . . for a while there I uploaded codeform version 1.2.0 to it at least twice a week, but now it doesn't get changed very much.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    a fast and easy c reference guide is what i needed

    nice work

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Don't cast the return value of malloc(), there is no need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 02:51 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. Dynamic array problem
    By new here in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2003, 06:39 PM
  4. Dynamic Array Problem
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 02:29 PM
  5. Dynamic array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2002, 10:55 AM

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