Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-10-2005, 12:12 AM   #1 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default 2d pointers, memset or how to initialise?


hi,

suppose i have

#....

main()
{
int n=6;(which means i am to generate a 6*6 matrix)
int **a;

and some how i allocated the memory required for 6*6 to the pointer a....

now, how do i initialise the elements of pointer a to "0" so that i can start accessing them as arrays without any junk values... coz, in my program some values r untouched and if i initialise to "0", it wud be fine, with junk its jus disappointing... i hav heard of memset command for 1d pointers, is it possible ot initialise 2d pointers using the same? thk u.

/legolas
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 06-10-2005, 01:05 AM   #2 (permalink)
Right Off the Assembly Line
 
gtoX's Avatar
 
Join Date: Jul 2005
Location: TecH_DepoT
Posts: 29
Default

Well, it depends on the reqd. speed of program you are creating.

You could create an initialisation portion inside the main() fnx., inside a for loop.
If it is time-critical, I suggest you use assembly codes in place of the for loop if your compiler supports it.

i think memset also works, because all it requires is the starting address(in your case, the int 'a'), and the total size of the array(6*6 = 36), irrespective of the dimension of the array. However, I haven't personally done that. You could try it out.
__________________
The trouble with wanting something is the fear of losing it .....or never getting it. [ Max Payne(2) ]

What is mind? No Matter..... What is matter? Never Mind [Homer Simpson]
gtoX is offline  
Old 06-10-2005, 12:24 PM   #3 (permalink)
Wise Old Owl
 
aadipa's Avatar
 
Join Date: Feb 2004
Location: Palghar, Mumbai
Posts: 1,000
Default

In reality, you will not need pointers to pointers (2D pointers) to store a matrix, a simple pointer will do, all you need is a mapping function.

Say I want to store a[6][6] in *p, then a[i][j] is *(p+(i*columes)+j)

A real use of pointer to pointer is when you need to exchange the rows in the matrix, or when you have to store array of different lengths and need a same mapping function.

In that case
Code:
int **p;

p = (**int) malloc(sizeof(int *)*ROWS);
for(i=0; i<ROWS; i++) 
{
    *(p+i) = (*int) calloc(sizeof(int), COLS); // calloc will set all to 0
}

*(*(p+2)+4) = 5;  // Same as  a[2][4] = 5
Here the mapping function becomes a[i][j] = *(*(p+i)+j)


I have not checked the code as I don't have any C compiler on my desk right now, but I think it is correct. Just try and reply. i hope that was what you wanted.
aadipa is offline  
Old 06-10-2005, 11:36 PM   #4 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default

@aadipa,

thks for the initialisation help, but i needed 2d pointers coz, i am using arrays of size 1000*1000 or more even and so i cant declare them static... the stack memory gts exceeded, so i got 2 make sure that i free them asa their need is over.. and moreover, with pointers, the stack memory usage is also better... i tried a different initialisation thou, it worked, and i am sure the initialisation i gave wud work either, coz i understood a lots abt these owing to that implementation.. thks for ur help.

/legolas
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Sujeet
- by clinton
- by Who
- by bhaskar
- by soumya

Advertisement




All times are GMT +5.5. The time now is 11:43 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2