LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-16-2005, 07:50 PM   #1
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Rep: Reputation: 30
c array of pointers


I can assign a new character to my array of strings:

Code:
int main()
{
  char *days[] = {"monday", "tuesday", "wednesday", "thursday",
		  "friday", "saturday", "sunday"};

  days[0][0] = 'v'; /* here I  can aisgn */

  int size = sizeof(days) / sizeof(char *);
  int i;
  for (i = 0; i < size; i++)
    printf("%s\n", *(days + i));
  tableReverse(days, size);
but after I pass a pointer to tableReverse() function and I try to assign something I get a segmentation fault.

Code:
void tableReverse(char *table[], int size)
{
  table[0][0] = 'q'; /* here I get a segmentation fault */
Why is it like that?
 
Old 08-16-2005, 09:43 PM   #2
sxzzsf
LQ Newbie
 
Registered: Aug 2005
Location: Jiangsu Nanjing, China
Distribution: slackware-current
Posts: 9

Rep: Reputation: 0
days[0][0] = 'v'; /* here I can aisgn */
<===
Can not assign too.
The address of days[0][0] is the same to which of table[0][0] in function tableReverse() and it is located in the read-only memory area. If write to this area, there will be Segmentation fault

Last edited by sxzzsf; 08-16-2005 at 09:49 PM.
 
Old 08-16-2005, 11:22 PM   #3
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by sxzzsf
days[0][0] = 'v'; /* here I can aisgn */
<===
Can not assign too.
The address of days[0][0] is the same to which of table[0][0] in function tableReverse() and it is located in the read-only memory area. If write to this area, there will be Segmentation fault
I double checked and I'm sure that I can assign in the first case. No segmentation fault and characters get changed.
 
Old 08-16-2005, 11:32 PM   #4
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
How big is char * days[]
How big is char * table[]

Check your allocations.
 
Old 08-16-2005, 11:36 PM   #5
sxzzsf
LQ Newbie
 
Registered: Aug 2005
Location: Jiangsu Nanjing, China
Distribution: slackware-current
Posts: 9

Rep: Reputation: 0
void tableReverse(char *table[], int size)
{
printf("0x%8x\n",&table[0][0]);
}

int main()
{
int size,i;
char *days[] = {"monday","tuesday", "wednesday","thursday","friday", "saturday", "sunday"};

size = sizeof(days) / sizeof(char *);
for (i = 0; i < size; i++)
printf("%s\n", *(days + i));
tableReverse(days, size);
printf("0x%8x\n",&days[0][0]);
printf("0x%8x\n",&days[0]);
return 0;
}
=============
after compiling using gcc 3.3.1
monday
tuesday
wednesday
thursday
friday
saturday
sunday
0x 804852f <== days[0][0]
0x 804852f <== table[0][0]
0xbffff8f0 <== days
 
Old 08-17-2005, 02:09 AM   #6
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
OK, that proves that the pointer(address) is the same. But I still don't understand why I can assign to days[0][0] and not to table[0][0].
 
Old 08-17-2005, 02:23 AM   #7
sxzzsf
LQ Newbie
 
Registered: Aug 2005
Location: Jiangsu Nanjing, China
Distribution: slackware-current
Posts: 9

Rep: Reputation: 0
But I can not
 
Old 08-17-2005, 04:36 AM   #8
madhukirant
LQ Newbie
 
Registered: Aug 2005
Posts: 2

Rep: Reputation: 0
segmentation fault

looks like you got it all wrong you cannot assign the value at either of the places...the segementation fault comes at the statement

days[0][0] = 'v';


when a array of pointers is initialised it becomes a constant pointer and you cannot change the value....
 
Old 08-17-2005, 07:37 AM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
I also don't see how you are possibly not getting a segmentation fault on the first assignment. Just to be sure, I built it on my system and in fact do get a segmentation fault with just the following code:

Code:
int main()
{
    char *days[] = {"monday", "tuesday", "wednesday", "thursday",
        "friday", "saturday", "sunday"};

    days[0][0] = 'v'; /* here I  can aisgn */
    return 0;
}
The problem here is you are attempting to change the value of a const char array ("monday" for example, is a const char array). I'm guessing you'd have more luck with something like this:

Code:
int main()
{
    char *days[] = {"monday", "tuesday", "wednesday", "thursday",
        "friday", "saturday", "sunday"};


    int size = sizeof(days) / sizeof(char *);
    int i;
    for (i = 0; i < size; i++)
        printf("%s\n", *(days + i));
    days[0] = "blahday"; /* here I  can aisgn */
    for (i = 0; i < size; i++)
        printf("%s\n", *(days + i));
    return 0;
}

Last edited by jtshaw; 08-17-2005 at 07:40 AM.
 
Old 08-17-2005, 09:38 AM   #10
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
Ok, so I can't assign it.
You, guys are correct.
I pasted the line 'days[0][0] = 'v'; /* here I can aisgn */' in a wrong spot in my post.
Actually I was assigning after calling reverseTable(), where memory is allocated for the reversed strings. I didn't know at first that the first declaration of 'days' makes it constant though.

So this is the program.

Code:
#include <stdlib.h>
#include <stdio.h>

void tableReverse(char *table[], int size);

int main()
{
  char *days[] = {"monday", "tuesday", "wednesday", "thursday",
		  "friday", "saturday", "sunday"};

  // days[0][0] = '!'; segmentation fault here

  int size = sizeof(days) / sizeof(char *);
  int i;
  for (i = 0; i < size; i++)
    printf("%s\n", *(days + i));

  tableReverse(days, size);

  days[0][0] = '&';  // can assign here
  days[1][0] = '*';
  days[2][1] = '$';

  for (i = 0; i < size; i++)
    printf("%s\n", *(days + i));

  return EXIT_SUCCESS;
}


void tableReverse(char *table[], int size)
{
  // table[0][0] = 'q'; segmentation fault here
  int j;
  for (j = 0; j < size; j++){
    int len = strlen(*(table + j));
    char *str = (char *)malloc(len);
    int i;
    for (i = 0; i < len; i++)
      str[i] = *(*(table + j) + (len - 1 - i));
    str[len] = '\0';
    *(table + j) = str;
  }
}
 
Old 08-17-2005, 10:25 AM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Whenever you have a string literal (e.g. "monday", "tuesday", etc.) that string literal is stored in a READ-ONLY data section of your executable. You should NEVER, EVER attempt to write to this memory.

If instead, you declared your array of days as a multidimensional array instead of an array of pointers, then memory that can be written to will be declared for you on the stack...

e.g.
Code:
char days[7][10] = {"monday", "tuesday", "wednesday", "thursday",
    "friday", "saturday", "sunday"};
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
SIGSEGV: Array of character pointers?? usercsr Programming 2 04-23-2005 11:34 AM
array of char pointers djgerbavore Programming 2 01-08-2005 01:59 PM
a question about array pointers in C veritas Programming 12 07-18-2004 06:31 PM
Pointers Pointers Pointers urzumph Programming 9 03-11-2004 09:49 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM


All times are GMT -5. The time now is 05:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration