You never set p back to the start of the string. I don't see where you call free() which you should learn to do. I just gave this code a quick look.
And therefore:
May I suggest something that will make your efforts easier?
There are several string functions that live in the
header file: strdup is one.
You should be using those functions, not rolling your own, given the way you have written your code.
Try:
to locate some interesting library functions,
then read then man page for
and some other very helpful C library functions
Learn about strcpy, strstr, strchr, strdup - there are several other good ones to know, too. You decide.
The initializer in char* tmp = "hello"; is a string constant. The C compiler is allowed to store string constants in read-only memory. If you want to overwrite a string, that string cannot be a string constant.
I am scared of Don Cragun, because he knows everything ;-).
Quote:
you can't do that, because gcc places the literal "hello" of char* tmp = "hello" in a read-only data segment, but char tmp[] = "hello" not.
Yes, interesting. So because that array stores actual memory on the stack, you cannot change what it points to, hmmmm. I thought arrays were pointers, until I tried to assign an array (pointer) to something else ;-). [I mean the reverse, assign something else to that array].
I am scared of Don Cragun, because he knows everything ;-).
Yes, interesting. So because that array stores actual memory on the stack, you cannot change what it points to, hmmmm. I thought arrays were pointers, until I tried to assign an array (pointer) to something else ;-). [I mean the reverse, assign something else to that array].
Don't be scared of me! I don't know everything, as I have unfortunately proven in earlier posts in this forum (but I do try to admit when I make mistakes).
Let me expand a little on what dodona and I have said in earlier posts...
Inside a function definition (such as in main() shown in post #1 in this thread), the declarations in main():
create:
an integer named iv on the stack and initializes it to the value 10,
a pointer named mynewbook that can be used to access a structure of type book but does not allocate any space for a structure of that type and the value assigned to that pointer will be any random value found on the stack where that pointer is located,
an integer named len containing whatever random value is located on the stack at the address assigned to that integer,
a pointer named words1 that can be used to access an object of type char that points to a random address depending on whatever value is located on the stack at the address assigned to that pointer,
a pointer named initial_words that can be used to access an object of type char that points to the first character of the string "hello_pleased_to_met_you" which might be located in read-write memory on the stack, in read-only memory that is not located on the stack, or in read-write memory that is not located on the stack, and
a pointer named tmp that can be used to access an object of type char that points to the first character of the string "hello" which might be located in read-write memory on the stack, in read-only memory that is not located on the stack, or in read-write memory that is not located on the stack.
With most modern compilers the strings mentioned in points 5 and 6 above will be located in read-only memory and will, therefore, generate a segmentation fault if you try to change the data in those strings.
Early C compilers (in the 1970's and 1980's) frequently put these arrays in read-write memory. And when you had code that tried to overwrite these strings, they succeeded. This had the side-effect of turning string constants into variables whose constant string values were not constants while the process was running.
To create an array of characters on the stack that can be read and written instead of a pointer on the stack that points to an array of characters that might be read-only, you need to use a declaration more like:
Both of these create arrays of characters on the stack. The constant string initializers will be copied into these arrays (on the stack) every time the function is invoked.
Arrays of characters and pointers to characters are two very different things. An array of characters has a size that is the number of characters that can be stored in it. A pointer to a character (or a pointer to an array of characters) has a constant size (usually 4 bytes per pointer on a system with a 32-bit address space or 8 bytes per pointer on a system with a 64-bit address space). You can increment a pointer to point to the next element in the array to which it points. You can't increment an array (although you can increment elements of an array). Although an array name is not a pointer, C allows an array name used without following square brackets to be used as a synonym for the address of the first element of that array. So, if I had the declarations:
then both of the following lines of code set the pointer tmp to point to the h in the string hello:
To then update the pointer to point to the next character in the array, you can use any of the following lines of code:
but you can't use either of:
because tmp_array is an array; and an array is not a pointer type.
Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1...
I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one.
Below are two very condensed snippets of which I have added the results inside the each code section.
IMPORTANT!... (11 Replies)
I am getting Segmentation fault at below function call in my script:
get_x() {
sqlplus -s / <<end | grep KEEP | sed 's/KEEP//;s///g'
select 'KEEP' ,table_name from all_synonyms where upper(synonym_name)= '$1';
exit
end
x=$(get_x $1)
echo " SQL OUTPUT IS :: $x"
}
I am getting output of... (1 Reply)
I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this :
i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
I am working on the application in which I have to fetch values from the database and paste in url and send it to portal.
table=get_result("SELECT serialno,cas,Mode,FLC,TLC,location,CompName,CompCode,FG,FC,DispNo,TruckNo,LWbill,RRGPN,INVNO,DCN,RQTY,DQTY,SQTY,DDATE,RDATE,SDATE,TTIME FROM... (1 Reply)
void disptree(node *ptr)
{
if ((ptr->left) !=NULL)
disptree(ptr->left);
cout<<"Position:"<<ptr->pos<<" Data:"<<ptr->data<<endl;
if ((ptr->right)!=NULL;
disptree(ptr->right);
}
i'm getting a segmentation fault at the red line. i cannot understand what's the problem.... (3 Replies)
Hi,
my code is written in proC and it is in UNIX(AIX).I have written a small code for writing data into a binary file,but while writing my program is giving core dump.
Here Is my code----
fpWriteFile = fopen(WriteFileName,"wb+");
CHAR *recvgen;
recvgen = (char... (7 Replies)
If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
int my_malloc(int size, char **pmem)
{
*pmem=(char *)malloc(size);
if(*pmem==NULL)... (11 Replies)
If I do this.
Assume
struct life
{
char *nolife;
}
struct life **life;
// malloc initialization & everything
if(life->nolife == 0)
Would I get error at life->nolife if it is equal to 0.
wrong accession? (3 Replies)
Hello,
I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it:
char* getext(char *file)
{
char *extension;
int i, j;... (5 Replies)
I am tring to install Lotus Domino/Notes 5.0.5 on a AIX 4.3.3 server.
I go to run the cdrom/ibmpow/install and I get the following error.
Lotus Notes for Unix Install Program
---------------------------------------------
./install: 10088 Segmentation fault
This had Lotus Notes installed... (1 Reply)