C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-12-2006, 09:46 AM   #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.
breaka is offline   Reply With Quote
Old 06-12-2006, 09:50 AM   #2
Just Lurking
 
Dave_Sinkula's Avatar
 
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   Reply With Quote
Old 06-12-2006, 09:50 AM   #3
Even death may die...
 
Dante Shamest's Avatar
 
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   Reply With Quote
Old 06-12-2006, 09:54 AM   #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   Reply With Quote
Old 06-12-2006, 10:01 AM   #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   Reply With Quote
Old 06-12-2006, 10:01 AM   #6
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 5,006
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.*
Dave_Sinkula is offline   Reply With Quote
Old 06-12-2006, 10:11 AM   #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
breaka is offline   Reply With Quote
Old 06-12-2006, 10:56 AM   #8
and the hat of Jobseeking
 
Salem's Avatar
 
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 );
	  }
}
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 06-12-2006, 11:10 AM   #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.
breaka is offline   Reply With Quote
Old 06-12-2006, 11:16 AM   #10
and the hat of Jobseeking
 
Salem's Avatar
 
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]);
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.

Salem is offline   Reply With Quote
Old 06-12-2006, 11:21 AM   #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   Reply With Quote
Old 06-12-2006, 12:20 PM   #12
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 5,006
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.*
Dave_Sinkula is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22