• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

pointer arrays

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
btocakci
Asked:
btocakci
  • 7
  • 5
1 Solution
 
sunnycoderCommented:
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
 
btocakciAuthor Commented:
"proje.exe has encountered with a problem and must be closed.." sayin window was opened and program was closed..

0
 
btocakciAuthor Commented:
i encounter with no errors or warnings in microsoft Visual studio compiler..
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
sunnycoderCommented:
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
 
btocakciAuthor Commented:
     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
 
btocakciAuthor Commented:
of course i wrote char*from="from" at the beginning..
0
 
grg99Commented:
have you initilized ptrarray's elements to point to some char[] array memory?

0
 
btocakciAuthor Commented:
i think i dont know how to do this?!
0
 
sunnycoderCommented:
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
 
btocakciAuthor Commented:
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
 
sunnycoderCommented:
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
 
btocakciAuthor Commented:
You will really teach me how to code in c by just yourself i think..Really thanks..
0
 
sunnycoderCommented:
Glad to help :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now