Skip to content

strcpy

Copies a string literal or string variable to another string variable.

strcpy and string assignment with = copy at initialization only. Use strcpyk to copy a string during performance.

Syntax

Sdst = strcpy(Ssrc)
Sdst = Ssrc
Sdst strcpy Ssrc
Sdst = Ssrc

Initialization

Ssrc -- the source string, given as a literal or a string variable.

Sdst -- the destination string variable. It receives a copy of the source. Later changes to the source leave this copy unchanged.

Examples

These examples copy a literal into a source variable, then copy it with both strcpy and =. They change the source and print all three strings. They run without audio output or external files.

strcpy-modern.csd

Copy strings at initialization
<CsoundSynthesizer>
<CsOptions>
-n -d -m0
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

instr 1
  ; Copy a literal, then copy that variable to two destinations.
  source:S = strcpy("Hello, world!")
  copied:S = strcpy(source)
  assigned:S = source

  ; Changing the source leaves both copies unchanged.
  source = strcpy("Goodbye.")
  prints("Source = %s\n", source)
  prints("Copy = %s\n", copied)
  prints("Assignment = %s\n", assigned)
endin
</CsInstruments>
<CsScore>
i 1 0 0.1
e
</CsScore>
</CsoundSynthesizer>

strcpy.csd

Copy strings at initialization
<CsoundSynthesizer>
<CsOptions>
-n -d -m0
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

instr 1
  ; Copy a literal, then copy that variable to two destinations.
  Ssource strcpy "Hello, world!"
  Scopy strcpy Ssource
  Sassigned = Ssource

  ; Changing the source leaves both copies unchanged.
  Ssource strcpy "Goodbye."
  prints "Source = %s\n", Ssource
  prints "Copy = %s\n", Scopy
  prints "Assignment = %s\n", Sassigned
endin
</CsInstruments>
<CsScore>
i 1 0 0.1
e
</CsScore>
</CsoundSynthesizer>

Both examples print these values.

Source = Goodbye.
Copy = Hello, world!
Assignment = Hello, world!

See Also

strcpyk, String Manipulation Opcodes

Credits

Author: Istvan Varga
2005