?
Solved

pointer arrays

Posted on 2006-05-24
13
Medium Priority
?
264 Views
Last Modified: 2010-04-15
Hi! im trying to use one of the elements of an pointer array like *ptrarray[10] in strcopy function..

....
atomptr=strtok(string,"");

for(i=0;i<10;i++){
strcpy(ptrarray[i],atomptr);
atomptr=strtok(NULL;"");}


my goal is to put all of the atoms of a string into seperate strings..
....

but it doesnt run..What is the mistake here?..
0
Comment
Question by:btocakci
  • 7
  • 5
13 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16750368
Hi btocakci,
> atomptr=strtok(NULL;"");}
there should be a comma (,) and not semi-colon between NULL and "" 
atomptr=strtok(NULL,"");}

> but it doesnt run..What is the mistake here?..
What are the errors? Does the above rectify them?

Cheers!
sunnycoder
0
 

Author Comment

by:btocakci
ID: 16750418
"proje.exe has encountered with a problem and must be closed.." sayin window was opened and program was closed..

0
 

Author Comment

by:btocakci
ID: 16750423
i encounter with no errors or warnings in microsoft Visual studio compiler..
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 16750436
Hi btocakci,

This sounds like you are accessing some memory which you should be accessing or there is some logical error.

If the code is small, post it here and we will have a look at it. If the code is huge, try to run it through a debugger or place prints at several places to find out where the error is occuring

Cheers!
sunnycoder
0
 

Author Comment

by:btocakci
ID: 16750439
     atomptr=strtok(string,sep);


      i=0;      
      while((strcmp(atomptr,from))){
      
            printf("%s\n\n",atomptr);
            
                        atomptr=strtok(NULL,sep);

                        strcpy(selectarraytemp[i],atomptr);
      i++;
      }
      
      strcpy(selectarraytemp[0],talha);

want to put this atoms into strings while the word "from" encountered. May tyhere be any infinite loops?
0
 

Author Comment

by:btocakci
ID: 16750448
of course i wrote char*from="from" at the beginning..
0
 
LVL 22

Expert Comment

by:grg99
ID: 16750480
have you initilized ptrarray's elements to point to some char[] array memory?

0
 

Author Comment

by:btocakci
ID: 16750531
i think i dont know how to do this?!
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 680 total points
ID: 16750804
malloc memory for each pointer

ptr = malloc (strlen(string));

You can do this in the same loop

     i=0;    
     while((strcmp(atomptr,from))){
     
          printf("%s\n\n",atomptr);
         
                    atomptr=strtok(NULL,sep);

                    selectarraytemp[i] = malloc (strlen(atomptr)+1);

                    strcpy(selectarraytemp[i],atomptr);
     i++;
     }

Before exiting the program, you need to free all the memory that you allocated

for (j=0; j<i; j++)
       free(selectarraytemp[j]);
0
 

Author Comment

by:btocakci
ID: 16750869
really thanks sunnycoder, i have really much to learn from you..

error C2440: '=' : cannot convert from 'void *' to 'char *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast

on the line :       selectarraytemp[i] = malloc (strlen(atomptr)+1);

what may be the problem?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16750960
Try to use the hints in error messages and warnings to determine what the problem might be ;)
selectarraytemp[i] = (char *)malloc (strlen(atomptr)+1);
0
 

Author Comment

by:btocakci
ID: 16751010
You will really teach me how to code in c by just yourself i think..Really thanks..
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16751206
Glad to help :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question