![]() |
| | #1 |
| Registered User Join Date: Jun 2006
Posts: 45
| newbee: initializing arrays |
| breaka is offline | |
| | #2 |
| Just Lurking Join Date: Oct 2002
Posts: 5,006
| 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.* |
| Dave_Sinkula is offline | |
| | #3 |
| Even death may die... Join Date: Apr 2003 Location: Malaysia
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. |
| Dante Shamest is offline | |
| | #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. |
| breaka is offline | |
| | #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 |
| breaka is offline | |
| | #6 |
| Just Lurking Join Date: Oct 2002
Posts: 5,006
| Your function's signature should be like this. Code: int input (char p[10][50]) Code: int count = input(text); 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.* |
| Dave_Sinkula is offline | |
| | #7 |
| Registered User Join Date: Jun 2006
Posts: 45
| well actually i'm using 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 |
| breaka is offline | |
| | #8 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| > 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 );
}
}
|
| Salem is offline | |
| | #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:
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. | |
| breaka is offline | |
| | #10 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| > 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]); |
| Salem is offline | |
| | #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 |
| breaka is offline | |
| | #12 | |
| Just Lurking Join Date: Oct 2002
Posts: 5,006
| Quote:
For instance, none of these is correct. 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.* | |
| Dave_Sinkula is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Initializing arrays in a class | ejohns85 | C++ Programming | 2 | 03-19-2009 09:06 AM |
| initializing multi-dimensional arrays | cyberfish | C++ Programming | 4 | 10-31-2007 08:15 PM |
| Initializing dynamic arrays | cunnus88 | C++ Programming | 9 | 11-21-2005 09:50 AM |
| initializing values for arrays, and chaging them w/o using scanf | sly_ | C Programming | 14 | 03-17-2003 10:07 AM |
| initializing char arrays to null | swiss powder | C++ Programming | 6 | 02-28-2002 02:56 PM |