By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

strncpy core dump

P: 2
I am getting core dump on strncpy on the following code. I could get strncpy to work only with array and not with pointer. Can somebody take a look at this code and suggest me why its core dump in strncpy

Thank you



Expand|Select|Wrap|Line Numbers
  1.  #include <string.h>
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6.  
  7. char* xmlData = "<Hello> this is a  site of the company   "; 
  8. char* startTag = "<test>";
  9. char* endTag = "</test>";
  10. char* startLocation;
  11. char* endLocation;
  12. char* tempString=NULL;
  13. int tempLength;
  14.  
  15. while(strstr(xmlData,startTag)!=NULL)
  16. {
  17. startLocation = strstr(xmlData,startTag)+strlen(startTag);
  18. endLocation = strstr(xmlData,endTag); 
  19. tempLength = endLocation - startLocation; 
  20. printf(" Start Loc = %s\n",startLocation); 
  21. printf("tempLength = %d\n",tempLength); 
  22. strncpy(tempString,startLocation,tempLength);
  23. }
  24. }
Aug 2 '07 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
char* tempString=NULL;
You are trying to copy to a NULL pointer.

Allocate memory for temp befiore you strncpy().
Aug 2 '07 #2

P: 2
Thank you, that's what I was missing
Aug 2 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.