Thread: newbee: initializing arrays

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    45

    newbee: initializing arrays

    hi, how do you initialize empty (multi)dimensional arrays correctly cause i'm getting general protection errors when using arrays in a function and i think its cause i'm not initializing them.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    char text[10][50] = {0};
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Initialize them to what? If it's 0, you can use memset.

    It also depends if your multidimensional arrays are created using [][] or using pointer-to-pointers.

    It would help if you post a sample code for us to compile.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    empty arrays using [],

    i wrote a simple text input function (stops asking for text when hitting enter only):

    here you go:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    void input (char *);
    
    int main (void){
    
    	  char text[10][50]={0};       //EDIT
    
    	  input(&text[0][0]);
    
    return 0;
    }
    
    void input (char *p){
    
    	  int i,sw=0;
    
    	  for(i=0; i<10 && sw!=1; i++){
    
    			printf("%d",p);
    
    			scanf("%[^\n]\n",*(p+i*50));
    			if (*(p+i*50)==0)
    				sw=1;
    	  }
    }
    Last edited by breaka; 06-12-2006 at 10:02 AM.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    initialisation doesnt seem to cause the general protection error, is there a way to diagnose them

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Your function's signature should be like this.
    Code:
    int input (char p[10][50])
    And you'd call it like this.
    Code:
    int count = input(text);
    Now stop doing this.
    Code:
    scanf("%[^\n]\n",*(p+i*50));
    And read the FAQ on string input.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    well actually i'm using
    Code:
    scanf("%[^\n]%*c",*(p+i*50));
    with %*c instead of \n but i guess thats wrong also;
    i used this code to enter lines of text with interuption when hitting enter only. Guess theres a better way then. Is it the use of pointers?

    checkin out the faq now

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > void input (char *);
    Is the only reason for doing this because you don't know how to pass multi-dimensional arrays to a function?

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void input (char a[][50]);
    
    int main (void){
      char text[10][50]={0};       //EDIT
      input(text);
      return 0;
    }
    
    void input (char p[][50]){
    	  int i,sw=0;
    
    	  for(i=0; i<10 && sw!=1; i++){
    			fgets( p[i], sizeof p[i], stdin );
    	  }
    }
    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.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    to salem: yes, i simply didn't know that (yet). i tought giving adress of first element in array was enough.
    1)why only defining one dimention's length?

    to dave:
    Quote Originally Posted by Dave_Sinkula
    Now stop doing this.
    Code:
    scanf("%[^\n]\n",*(p+i*50));
    2)wich part should i stop using? fgets in stead of scanf?


    3)Is there a way for newbees to diagnose general exeption errors by there numbers. when do those errors occur actually?
    Last edited by breaka; 06-12-2006 at 03:08 PM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > fgets in stead of scanf?
    In almost all cases, it's much simpler and safer to use fgets() to read into some temporary buffer, where you can then validate and convert the data as appropriate, before finally storing the data in it's final location.

    > why only defining one dimentions length?
    You have to define ALL the dimensions except for the left-most one (which is optional).

    All these prototypes are equivalent.
    Code:
    void input (char text[10][50]);
    void input (char text[][50]);
    void input (char (*text)[50]);
    Note that the first one is simply a copy/paste of the array you want to pass.
    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.

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    thank you guyz, lot a work to do here, is there a simple answer to question 3

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by breaka
    3)Is there a way for newbees to diagnose general exeption errors by there numbers. when do those errors occur anyway?
    Fix all errors and warnings in the code first so that it is 99% unlikely you encounter one.

    For instance, none of these is correct.
    Code:
    printf("%d",p);
    scanf("%[^\n]\n",*(p+i*50));
    scanf("%[^\n]%*c",*(p+i*50));
    Crank up the warning level; hopefully it will then point these out to you.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing arrays in a class
    By ejohns85 in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2009, 09:06 AM
  2. initializing multi-dimensional arrays
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2007, 08:15 PM
  3. Initializing dynamic arrays
    By cunnus88 in forum C++ Programming
    Replies: 9
    Last Post: 11-21-2005, 09:50 AM
  4. Replies: 14
    Last Post: 03-17-2003, 10:07 AM
  5. initializing char arrays to null
    By swiss powder in forum C++ Programming
    Replies: 6
    Last Post: 02-28-2002, 02:56 PM