Welcome, Guest. Please login or register.

Author Topic: Understanding Strings  (Read 1509 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline DrkStarrTopic starter

  • Newbie
  • *
  • Join Date: Aug 2016
  • Posts: 4
    • Show only replies by DrkStarr
Understanding Strings
« on: August 30, 2016, 02:49:28 PM »
Hi everybody,
I've installed the latest SDK 53.30 on top of AMIGAOS 4.1 FE.
I'm compiling in the Shell, using GCC version 4.2.4.

The problem I'm having is with strings.  I don't really understand them.

I've looked around and I can't even do a strcpy.  It's pretty frustrating.

Code: [Select]

#include <stdio.h>
#include <string.h>
#include <proto/intuition.h>

UBYTE *taxString = &quot;100%&quot;;
UBYTE *taxPass = &quot;10%&quot;;

int void(main) {
    strcpy(taxString, taxPass);
    printf (&quot;Tax String: %s\n&quot;, taxString);
}


I can get the code to compile in GCC but it throws a Guru Meditation Error when I run it.  

I'm also placing taxString in a StringObject and it's throwing the same Guru.

I'm not sure what I'm doing wrong.  I've played around with the code a lot, but I can't get it to work.

- Thanks for the help
 

guest11527

  • Guest
Re: Understanding Strings
« Reply #1 on: August 31, 2016, 05:30:52 AM »
Quote from: DrkStarr;813224
The problem I'm having is with strings.  I don't really understand them.
C doesn't really have strings as first class citizens. It has character arrays, and pointers to characters.

Code: [Select]
UBYTE *taxString = &quot;100%&quot;;
UBYTE *taxPass = &quot;10%&quot;;
What this code does is the following: "100%" is a string literal for which storage in the "text" region (write-only memory) is allocated. The type of the right hand side is "const char [5]", i.e. an array of characters of length 5, each of which is constant. The pointer taxString is initialized to point to the first character of this array.

gcc *should* actually warn here (compile with "-Wall", please), because you now initialize a pointer to UBYTE pointing to a const char [5], which is the type of the literal. First, "UBYTE" != "char", and second, the source is constant, and the pointer is not. The former is not much of a problem, but the latter means that you are not allowed to modify elements of the entry.

Code: [Select]
strcpy(taxString, taxPass);
This code now writes(!) into a constant memory region, and lukely, the Os pukes about it. That certainly does not work. You cannot assign to constant data.

What you want to do instead is to create first an array of characters that is long enough to hold the longest possible string you possibly ever want to assign to it, and then initialize it. For example, if you know that you need strings of at most 99 characters:

Code: [Select]
char taxString[100] = "100%";
const char *taxPass = "10%";
This does something different, especially the first line. Actually, this line does two things: a) it creates an array of characters that is 100 elements in size, so it can hold strings of length 99 and below (yes, 99, not 100!). b) it initializes this array with the string "100%" by copying the string literal into the array when starting the program.

*Now* you can copy into this array. If you copy a string longer than 99 characters into this array, still strange and wonderful things may happen. C does not protect you from doing that, unfortunately. But in your example, you don't do that, so you're fine.
 

Offline DrkStarrTopic starter

  • Newbie
  • *
  • Join Date: Aug 2016
  • Posts: 4
    • Show only replies by DrkStarr
Re: Understanding Strings
« Reply #2 on: August 31, 2016, 05:42:01 PM »
Thanks a lot, that worked.

I thought it might have something to do with the declaration, but there were so many different examples out there I couldn't figure it out.

And I guess I should start using -Wall.

- Really appreciate it :)