?
Solved

question on using strstr and strncpy

Posted on 2008-11-05
2
Medium Priority
?
824 Views
Last Modified: 2008-11-05
Hello,

I'm using following code but in runtime I get a segmentation fault message.  I'm trying to learn how find a substring in a string and replace with something or "".

Thanks.

#include <stdio.h>
#include <string.h>
 
int main(void)
{
 
  char* d1 = "http://www.test.com";
  char* d2 = "https://www.test.com";
  char* d3 = "https://test.com";
 
  char* pch;
  pch = strstr(d1,"http://");
  strncpy(pch, "xyz", 3);
 
  printf("\nresult = %s", pch);
 
  return 0;
}
~

Open in new window

0
Comment
Question by:akohan
2 Comments
 

Assisted Solution

by:dlinvill
dlinvill earned 100 total points
ID: 22889036
You are defining d1 as a pointer to a constant character string.  You can't then copy over it.

Try defining d1 like:    char d1[99] = "http://www.test.com";

This gives you an array of characters that you can then use as the destination in strncpy

0
 
LVL 6

Accepted Solution

by:
Werrock earned 400 total points
ID: 22889053
You cannot replace data like that. The d1 is not a buffer only a pointer. If you did:
char d1[100] = "http://www.test.com";
char *pch  = strstr(d1,"http://");
strncpy(d1, "xyz", 3);
printf("\nresult = %s", pch);

It would "work" but not as intended since its not a true replace since your only overrideing some data in the buffer, it the string length is different this wont work. There is no function call to do it right away and you need to copy or move some data for it. Read more about it here:
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21258062.html
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

829 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