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.
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.
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.*
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.
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.
initialisation doesnt seem to cause the general protection error, is there a way to diagnose them
Your function's signature should be like this.And you'd call it like this.Code:int input (char p[10][50])Now stop doing this.Code:int count = input(text);And read the FAQ on string input.Code:scanf("%[^\n]\n",*(p+i*50));
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.*
well actually i'm usingwith %*c instead of \n but i guess thats wrong also;Code:scanf("%[^\n]%*c",*(p+i*50));
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
> 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.
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:2)wich part should i stop using? fgets in stead of scanf?Originally Posted by Dave_Sinkula
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.
> 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.
Note that the first one is simply a copy/paste of the array you want to pass.Code:void input (char text[10][50]); void input (char text[][50]); void input (char (*text)[50]);
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.
thank you guyz, lot a work to do here, is there a simple answer to question 3
Fix all errors and warnings in the code first so that it is 99% unlikely you encounter one.Originally Posted by breaka
For instance, none of these is correct.
Crank up the warning level; hopefully it will then point these out to you.Code:printf("%d",p); scanf("%[^\n]\n",*(p+i*50)); scanf("%[^\n]%*c",*(p+i*50));
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.*