Forum > Spanish

Limite String

(1/12) > >>

JuanCP:
Un saludo a todos.

Siguiendo con mis practicas de FP me he encontrado con una situación que no se resolver.

program cadena;

{$MODE CONSOLE}
{$R+}

var
   frase: string;

begin
   frase := 'Esto es una prueba';
   writeln(frase);
   writeln(frase[30]);
end.


La variable frase tiene una longitud de 17 caracteres.

Si intento imprimir el nº 30, por ejemplo, el compilador no lanza ningún error o advertencia y ejecuta el programa sin problemas.

Tanto delphi, Java o Python dan error al intentar acceder a un elemento mayor.

Gracias de antemano

KodeZwerg:
I do not know what you are writing but your code produce an access violation.
with "writeln(frase[30]);" you try to access string position #30 what does not exist.
If you want a fixed length string you can do:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program cadena; {$MODE CONSOLE}{$R+} var   frase: string[30];begin   frase := 'Esto es una prueba';   writeln(frase);   writeln(frase[30]);end.

Fred vS:

--- Quote from: JuanCP on September 08, 2022, 07:53:49 pm ---Si intento imprimir el nº 30, por ejemplo, el compilador no lanza ningún error o advertencia y ejecuta el programa sin problemas.

Tanto delphi, Java o Python dan error al intentar acceder a un elemento mayor.

Gracias de antemano

--- End quote ---

Hola!

No sé sobre Delphi pero esto:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  program cadena;    var       frase: string[16];    begin       frase := 'Esto es una prueba';       writeln(frase);       writeln(frase[30]);    end. 
da esto en tiempo de compilació.


--- Quote ---fred@fred-80m0 ~ [1]> fpc cadena.pas
Free Pascal Compiler version 3.2.2 [2021/07/09] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling cadena.pas
cadena.pas(6,14) Warning: String literal has more characters than short string length
cadena.pas(8,22) Warning: range check error while evaluating constants (30 must be between 0 and 13)
Linking cadena
8 lines compiled, 0.5 sec
2 warning(s) issued
fred@fred-80m0 ~>

--- End quote ---

Fred vS:
Re-holla!

Pero sí, eso no siempre funciona... :-[

Esto:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  program cadena;          var       frase: string = 'Esto es una prueba';    begin       writeln(frase);       writeln(frase[30]);    end.
Da esto sin "warning":


--- Quote ---fred@fred-80m0 ~> fpc cadena.pas
Free Pascal Compiler version 3.2.2 [2021/07/09] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling cadena.pas
Linking cadena
7 lines compiled, 0.2 sec

--- End quote ---



JuanCP:
Compiled with FP on sublime_text on no time I have   error of acces violation.

The second image is the result of a index correct

Navigation

[0] Message Index

[#] Next page

Go to full version