LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 02-11-2006, 10:24 PM   #1
drgilberto
LQ Newbie
 
Registered: Jan 2006
Location: St.Louis
Distribution: Ubuntu
Posts: 5

Rep: Reputation: 0
character arrays; tricky C language pointer problem!


I have this very simple piece of C code.

--------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char x[]="hello";
char *xx[] = {"hello","there"};

int main()
{
printf("%c\n",xx[0][0]);
x[0]='a'; /* this can be done */
xx[0][0]='x'; /* this fails!!! segfault will occur, but WHY */
return(0);
}
---------------------------------------

Compile with gcc and run it.
As you can see, it will segfault. You can change a value in the character value of variable x, but not in xx.

I don't get that!?

What could I do to be able to change values in the initialized variable xx.

Thanks!

Last edited by drgilberto; 02-11-2006 at 11:06 PM.
 
Old 02-11-2006, 11:37 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
well.. im a little drunk but im pretty sure that when you say
Code:
char *xx[] = {"hello","there"};
you are declaring an array of literals..

cant change a literal..
 
Old 02-12-2006, 04:30 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352

Rep: Reputation: 129Reputation: 129
Drunk or not xhi is correct , see this thread

You want to make xx an array of arrays.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash simple test with posix character class osio Programming 5 01-22-2006 07:23 PM
Python: Need help making a simple program using arrays and loops Baix Programming 11 08-13-2005 11:26 PM
Arrays of Functions and Classes problem. (C++) -=zAe=- Programming 2 02-24-2005 10:21 AM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 09:31 AM
exciting java arrays problem! Bert Programming 3 11-20-2001 05:11 AM


All times are GMT -5. The time now is 03:19 PM.

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
Open Source Consulting | Domain Registration