LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2007, 06:54 PM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 32
two-dimensional array in C: switching between pointer and index notation


Hi everyone,

I think this is an easy question, I am just a bit rusty. I want to have an array of strings, each string 3 characters long. So I declare it as
Code:
char (*name)[3];
If I am not mistaken, without parens this would be an array of 3 pointers to char, right?

OK so then I set aside some space for this array with:
Code:
name=malloc(N);
Then, when I want to assign values to the array elements, I want to switch to the index notation (it is more convinient for the rest of the code), so I write:
Code:
name[index][3]=some_short_string;
But gcc complains, saying
Code:
"warning: assignment makes integer from pointer without a cast".
Well, yeah, I can understand how it can be seen that way, but what's the right way to do it? Even when I stick with the pointer notation and do
Code:
*(name++)[3]=some_short_string;
it gives me the same warning. Thanks for your help.

Last edited by frankie_DJ; 02-22-2007 at 11:14 PM.
 
Old 02-22-2007, 11:07 PM   #2
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
Man this was wrong on so many levels. I can't really do assignements like that, have to use strcpy; and there is no need to mention the size [3] after initializing. So this compiles:
Code:
strcpy(*name,some_short_string);
But how do I switch to indices?

Last edited by frankie_DJ; 02-22-2007 at 11:14 PM.
 
Old 02-22-2007, 11:22 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I'm just tried this on my box:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef mystring[4]; /* Each string has three characters (plus one for NULL) */

int main( int argc, char *argv[] )
{
  mystring *names;

  names = malloc( sizeof(mystring) * 3 );

  strcpy( names[0], "joe" );
  strcpy( names[1], "bob" );
  strcpy( names[2], "jim" );

  printf( "%c%c%c\n", names[0][0], names[1][1], names[2][2] );

  free( names );

  return 0;
}
 
Old 02-22-2007, 11:44 PM   #4
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
for using indices u need to use loop which will copy character by character.
and the relation b/w pointers and indices is :

a[0] is same as *(a+0)
a[1] is same as *(a+1)... and so on.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Maximum 4 dimensional array size disruptive Programming 26 02-06-2007 10:51 PM
two dimensional array sorting in C/C++ George2 Programming 10 11-19-2006 08:29 PM
Bash 2 dimensional array ldp Linux - General 5 07-29-2005 11:29 AM
3 dimensional array help leeach Programming 11 06-15-2005 10:46 AM
pointer notation vs array notation? pablowablo Programming 5 03-14-2005 12:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration