LinuxQuestions.org
Help answer threads with 0 replies.
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 07-30-2012, 12:24 AM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
char * ( *(array_base_address + 2 ) + 3 )


char *array_base_address[] = { "roshni" , "manish" , "sona" , "bijjuda" , "ritu"};

i know that this is used for muti-dimensional array . but i have a a problem that when *(array_base_address + 2 ) is processed inside char * ( *(array_base_address + 2 ) + 3 ) , why "*(array_base_address + 2 )" is not pointing to s in "sona"
 
Old 07-30-2012, 12:33 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please produce a minimal but complete program, that shows the problem. (You might even try and use tags [code] and [/code] when paste it in.)
 
Old 07-30-2012, 12:47 AM   #3
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
#include<stdio.h>

int main()
{
char array_base_address[][7]={"Roshni","Manish","Sona","Bajju","Ritu"};

printf("%c",*(*(array_base_address+2)+2));

return 0 ;
}
 
Old 07-30-2012, 01:29 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well done. And now:
1. What did you expect to happen?
2. What did happen?
3. You really couldn't use [code] and [/code] before and after the code?

Note: your program would be easier to understand with the standard operators:
Code:
printf("%c",*(*(array_base_address+2)+2));
printf("%c",*(array_base_address[2]+2));
printf("%c",array_base_address[2][2]);
the output is 'n' from 'Sona'
 
1 members found this post helpful.
Old 07-30-2012, 05:23 AM   #5
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by NevemTeve View Post
Well done. And now:
1. What did you expect to happen?
2. What did happen?
3. You really couldn't use
Code:
 and
before and after the code?

Note: your program would be easier to understand with the standard operators:
Code:
printf("%c",*(*(array_base_address+2)+2));
printf("%c",*(array_base_address[2]+2));
printf("%c",array_base_address[2][2]);
the output is 'n' from 'Sona'
support what NevemTeve said above.

Learn with each other.

: )
 
Old 07-30-2012, 05:36 AM   #6
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
Sir i know i am wrong , but why not this !

printf("%c",*(*(array_base_address+2)+2)) in this line , when ever *(array_base_address+2) is processed , so after processing why this why sona term is not taking the place of *(array_base_address+2)

like

printf("%c",*(*(array_base_address+2)+2)) , so after processing *(array_base_address+2) , why it is not acting like *(sona+2) .
because *(array_base_address+2) indicates the base address of 3rd row in the array !

Sir nevemteve & gcchen

printf("%c",*(*(array_base_address+2)+2));

think about this line , we have base address array_base_address now we are adding +2 in it means now we are at 3rd row of the array , and after it , we are saying *(array_base_address+2) , it means we are dealing with sona , now we are adding +2 in sona we can say like this , *(sona+2)
.....but this is wrong , because it is multidimensional array , i know it , but why this is wrong !

Last edited by tushar_pandey; 07-30-2012 at 05:48 AM.
 
Old 07-30-2012, 06:06 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please try to understand that *(array_base_address+2) is the same thing as array_base_address[2].

Note: nothing is wrong: you get the 3rd character of the 3rd row, just as you wanted.

Last edited by NevemTeve; 07-30-2012 at 06:07 AM.
 
Old 07-30-2012, 09:20 PM   #8
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
Smile

Sorry for bad format of the contents. please see next reply below, instead of.

Last edited by gchen; 08-01-2012 at 03:07 AM. Reason: delete it for bad format contents
 
Old 07-30-2012, 09:23 PM   #9
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
Smile

Code:
  1 #include <stdio.h>
  2 
  3 int main()
  4 {
  5         char *ptrnormal = NULL;
  6         char *ptroff = NULL;
  7         char (*ptranormal)[8] = NULL;
  8         char (*ptrael)[8] = NULL;
  9         char (*ptraoff)[8] = NULL;
 10 
 11         struct {
 12                 char array0[8];
 13                 char array1[8];
 14                 char array2[8];
 15 
 16         } wrap = {{'a', 'b', 'c', '\0'},
 17                   {'h', 'i', 'j', '\0'},
 18                   {'o', 'p', 'q', '\0'}};
 19 
 20         char arrayarray[3][8] = {{'A', 'B', 'C', '\0'},
 21                                  {'H', 'I', 'J', '\0'},
 22                                  {'O', 'P', 'Q', '\0'}};
 23 
 24         ptrnormal = wrap.array0;
 25         ptroff = wrap.array0 + 2;
 26         ptranormal = arrayarray;
 27         ptrael = &wrap.array0;
 28         ptraoff = &wrap.array0 + 2;
 29 
 30 
 31         fprintf(stdout, "\nptrnormal: %s\nptroff: %s"
 32                         "\n*ptranormal: %s\n*ptranormal + 2: %s"
 33                         "\n*(ptranormal + 2): %s"
 34                         "\n*ptrael: %s\n*ptraoff: %s\n",
 35                         ptrnormal, ptroff,
 36                         *ptranormal,
 37                         *ptranormal + 2,
 38                         *(ptranormal + 2),
 39                         *ptrael, *ptraoff);
 40         
 41         
 42         return 0;
 43 }
The output is:

[root@localhost test]# cc -g -Wall test.c
[root@localhost test]# ./a.out

ptrnormal: abc
ptroff: c
*ptranormal: ABC
*ptranormal + 2: C
*(ptranormal + 2): OPQ
*ptrael: abc
*ptraoff: opq
[root@localhost test]#


Hope these information is helpful for you.

: )
 
Old 08-01-2012, 03:09 AM   #10
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
Please read "Expert C Programming" book (can download from internet, please google).

It contents many information about Array and pointers.

: )
 
  


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
error: invalid conversion from ‘const char*’ to ‘char*’ zx_ Programming 2 09-06-2011 08:17 PM
invalid conversion from `const char*' to `char' error holystinken Programming 7 11-23-2009 06:01 PM
error: invalid conversion from const char* to char* Dahakon Programming 1 08-31-2009 09:33 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM

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

All times are GMT -5. The time now is 03:04 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