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 04-12-2007, 09:55 PM   #1
i.you
Member
 
Registered: Oct 2006
Posts: 46

Rep: Reputation: 15
simple pointer question


Hello.

Please look at this code:

int main(int argc, char **argv)
{
...
char **temp;

for(i=0; i<argc, i++) {
// *temp[i] = *argv[i];
strcpy(temp[i], argv[i]);
}
...


The segmentation faults occur in the for loop.
The number of argv is more than 300.
What's wrong?

Please help me.
 
Old 04-12-2007, 10:05 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
temp isnt initialized. so your trying to copy argv[i] to a place that hasnt been allocated memory.
 
Old 04-12-2007, 11:13 PM   #3
i.you
Member
 
Registered: Oct 2006
Posts: 46

Original Poster
Rep: Reputation: 15
Thanks.

but the seg faults also occus although allocate memory to temp, i.e.,

char **temp = (char **)malloc(SIZE);
 
Old 04-12-2007, 11:45 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
heres an example
Code:
#include<stdio.h>
#include<string.h>

int main()
{
    int SIZE = 1;
    int MAX_STR_LEN = 20;
    
    char* word = "hello there";
    
    char** temp = (char**) malloc(sizeof(char*) * SIZE);

    temp[0] = (char*) malloc(sizeof(char) * MAX_STR_LEN);
    
    strcpy(temp[0],word);
    printf("'%s'\n",temp[0]);
    
    free(temp[0]);
    free(temp);
    
    getchar();
}

Last edited by nadroj; 04-13-2007 at 12:11 AM.
 
Old 04-12-2007, 11:51 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by nadroj
please note: my post count does not mean i am a guru, i consider myself a linux newbie
The world could use a few more newbies like you!
 
Old 04-12-2007, 11:56 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60


/deleteme

Last edited by nadroj; 04-13-2007 at 12:12 AM.
 
Old 04-13-2007, 02:29 PM   #7
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 04-14-2007, 10:33 AM   #8
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,834

Rep: Reputation: 108Reputation: 108
Hya,

You allocated memory to **yourHandle,

What you have to allocate is *yourPointer.

**temp is a pointer to pointer, allocate memory to *temp. not **temp.
Code:
for(......)   *temp[i] = (char*)malloc(yoursize);
Happy Penguins
 
  


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
Simple array/pointer question. smoothdogg00 Programming 3 03-15-2006 10:15 AM
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
a simple pointer problem kapsikum Programming 2 03-09-2005 11:05 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM

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

All times are GMT -5. The time now is 04:23 PM.

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