Thread: Multidimensional array into function

  1. #16
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Thanks guys, I think it was the "type" that got me into trouble. I also used a function in order to read N first

    Code:
    int findN(FILE* input)
    {
    int N;
    fscanf(input, "%d", &N);
    return(N);
    }
    In terms or my malloc statement, yes I was trying to allocate memory for my array with 3*N space. How should I do this instead?

    Code:
    ptr = malloc(N*3*sizeof(double));
    Also, my code now works for the input I specified in my original post, but now I want to add a description line to it

    Code:
        4
    description that includes spaces up to 80 characters or less
    O   -0.906801    0.569837    0.066005
    O    0.956585   -0.258900    0.342064
    O   -0.962633    0.494832    0.380356
    O   -0.259093    0.292540    0.854958
    fscanf will only read to whitespace and I need to read it to a character string but what I tried didn't work...

    Code:
    char buf[81];
    
    fgets(buf, sizeof buf, input);
    printf("Description is %s", buf);
    any ideas would be great

    Thanks a lot

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You can use fgets to get a whole line, or you can use something like:
    Code:
    fscanf(input, "%[^\n]", buf);
    Note, that is untested, so double check against the scanf documentation for usage of %[].

  3. #18
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by anduril462 View Post
    You can use fgets to get a whole line, or you can use something like:
    Code:
    fscanf(input, "%[^\n]", buf);
    Note, that is untested, so double check against the scanf documentation for usage of %[].
    I have seen this around but have so far not been able to use it

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by browser View Post
    In terms or my malloc statement, yes I was trying to allocate memory for my array with 3*N space. How should I do this instead?

    Code:
    ptr = malloc(N*3*sizeof(double));
    That's correct, depending what you want to do with ptr. That will give you access to a one dimensional array with N*3 elements. If you want a two dimensional array:

    Code:
    	int i;
    	double **ptr = malloc(N*sizeof(double*));
    	for (i = 0; i<N; i++) {
    		ptr[i] = malloc(3*sizeof(double));
    	}
    Notice the first dimension is an array of double*, and the second dimension are the actual doubles. Make sense? The significance here is WRT to indexing; you cannot refer to ptr[2][2] if ptr is one dimensional, you would have to refer to ptr[2*N+2].

    If use the 2D method, you must free each element of **ptr the same way.


    fscanf will only read to whitespace and I need to read it to a character string but what I tried didn't work...
    You can use a more customized template, eg:

    Code:
    %80[A-Z a-z0-9]
    That will read upto 80 characters of anything that include alphanumeric characters plus spaces (ie, the characters/ranges inside the square brackets).

    You may also want to look at fgets(), if the description is a line of its own, or an fscanf near equivalent:

    Code:
    "%[^\n]\n"
    Which will read everything except (^ means not) a newline; the read ends at the first rejected character. Then the trailing \n is to read that and discard.
    Last edited by MK27; 10-17-2011 at 12:44 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-02-2009, 03:07 AM
  2. Passing multidimensional/dynamic array to function
    By epyfathom in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 05:39 PM
  3. multidimensional array as a function argument
    By Luciferek in forum C++ Programming
    Replies: 7
    Last Post: 09-30-2008, 11:16 AM
  4. Help with passing multidimensional array to function by ref.
    By PatrickSteiger in forum C Programming
    Replies: 2
    Last Post: 11-19-2007, 02:37 PM
  5. multidimensional array function - simple question
    By boltz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2005, 04:24 PM