Thread: Pointers or Arrays?

  1. #1
    Registered User
    Join Date
    Jun 2008

    Pointers or Arrays?

    I am rewriting MatLab code in C and I need some direction on how to approach converting the code.

    Basically, a user inputs one of 6 scenarios. Each scenario has about 6 matrices with stored values. The problem is that each scenario uses different sized matrices. A has 1x2 and 1x1, where B has 2x8 and so forth...

    I originally wanted to use pointers, because for dynamic arrays, I was taught that pointers work better. However, if I declare pointers, how do I store the values into the pointer without having to retype
    p[x][y] = number
    for each element? As far as I know, if I declare a double pointer to a 2D matrix, I can't just say
    p[2][2] = {{a,b},{c,d}};
    So how do I do this without having to create multiple functions? I can't just declare new variables in my switch statement, because they won't work in the rest of the program, right?

  2. #2
    Join Date
    Oct 2007
    If the size varies, then you're going to have to go with a dynamic approach OR you can determine the maximum size of a matrice (might be simpler), thus being able to accommodate all.
    However, there's no general solution to the initialization problem because due to the dynamic data, you simply can't initialize it correctly for all scenarios. The best way is simply to determine the size and manually fill the array.
    If you've defined the array in the function scope, you can access it from anywhere inside the function and the data will remain valid.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2008
    So then I would have to declare the matrices as such...

    int row, col;
    and then make make the row and col actual integers for each separate case?

    Is that "proper" programming or is it not good to declare the size of a matrix without actual sizes yet? I thought of doing it this way, but I thought it looked messy and improper.

    My compiler happens to complain if I leave the array size empty, so I can't even do
    Doing it this way will also leave me with some
    matrices, which looks silly. It shouldn't make a difference though, right?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Quote Originally Posted by magda3227 View Post
    So then I would have to declare the matrices as such...

    int row, col;
    This code is incorrect, as row and col are not given a value yet - if the compiler accepts this, it will most likely crash because your values are "rubbish" [that is, if it's local variables - if it is globals, then row and col are zero, so not much use either].

    The further discussion is of course not much point, since the original concept is broken.

    Also, if we assume you do:
    int row, col;
    scanf("%d", &col);
    scanf("%d", &row);
    int matrix1[row][col];
    That would rely on compiler extensions that are not available in the standard (even C99), because the standard requires that the row and col values are constants - either const at compile-time, or const parameters to a function.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 09:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 10:13 AM