Skip to content
Snippets Groups Projects
Commit 6f64cf37 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Weitere Beispiele: C-Array zurückgeben, 5.6.2023

parent 5c63ff41
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
int* read_something () /* ohne "void": Funktion kann beliebig viele Parameter haben */
{
int n;
printf ("Wie viele Zahlen? ");
scanf ("%d", &n);
int b[n];
for (int i = 0; i < n; i++)
{
printf ("%d. Zahl: ", i + 1);
scanf ("%d", b + i); /* andere Schreibweise: &a[i] */
}
return b;
}
int main (void)
{
int *x = read_something (&x);
for (int i = 0; i < sizeof (x); i++)
printf (" %d", x[i]);
printf ("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int* read_something () /* ohne "void": Funktion kann beliebig viele Parameter haben */
{
int n;
printf ("Wie viele Zahlen? ");
scanf ("%d", &n);
int *b = malloc (n * sizeof (int));
for (int i = 0; i < n; i++)
{
printf ("%d. Zahl: ", i + 1);
scanf ("%d", b + i); /* andere Schreibweise: &a[i] */
}
return b;
}
int main (void)
{
int *x = read_something (&x);
for (int i = 0; i < sizeof (x); i++)
printf (" %d", x[i]);
printf ("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int* read_something (int *nn)
{
int n;
printf ("Wie viele Zahlen? ");
scanf ("%d", &n);
int *b = malloc (n * sizeof (int));
for (int i = 0; i < n; i++)
{
printf ("%d. Zahl: ", i + 1);
scanf ("%d", b + i); /* andere Schreibweise: &a[i] */
}
if (nn)
*nn = n;
return b;
}
int main (void)
{
int n;
int *x = read_something (&n);
for (int i = 0; i < n; i++)
printf (" %d", x[i]);
printf ("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int *data;
int n;
}
dyn_array;
dyn_array read_something (void)
{
dyn_array a;
printf ("Wie viele Zahlen? ");
scanf ("%d", &a.n);
int *b = malloc (a.n * sizeof (int));
for (int i = 0; i < a.n; i++)
{
printf ("%d. Zahl: ", i + 1);
scanf ("%d", b + i); /* andere Schreibweise: &a[i] */
}
a.data = b;
return a;
}
int main (void)
{
dyn_array x = read_something ();
printf ("%d eingegebene Zahlen:", x.n);
for (int i = 0; i < x.n; i++)
printf (" %d", x.data[i]);
printf ("\n");
free (x.data);
return 0;
}
.file "array-param-12.c"
.text
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Wie viele Zahlen? "
.LC1:
.string "%d"
.LC2:
.string "%d. Zahl: "
.text
.globl read_something
.type read_something, @function
read_something:
.LFB22:
.cfi_startproc
pushq %r13
.cfi_def_cfa_offset 16
.cfi_offset 13, -16 ; #include <stdio.h>
pushq %r12 ; #include <stdlib.h>
.cfi_def_cfa_offset 24 ;
.cfi_offset 12, -24 ; typedef struct
pushq %rbp ; {
.cfi_def_cfa_offset 32 ; int *data;
.cfi_offset 6, -32 ; int n;
pushq %rbx ; }
.cfi_def_cfa_offset 40 ; dyn_array;
.cfi_offset 3, -40 ;
subq $24, %rsp ; dyn_array read_something (void)
.cfi_def_cfa_offset 64 ; {
leaq .LC0(%rip), %rdi ; dyn_array a;
movl $0, %eax ; printf ("Wie viele Zahlen? ");
call printf@PLT ; scanf ("%d", &a.n);
leaq 8(%rsp), %rsi ; int *b = malloc (a.n * sizeof (int));
leaq .LC1(%rip), %rdi ; for (int i = 0; i < a.n; i++)
movl $0, %eax ; {
call __isoc99_scanf@PLT ; printf ("%d. Zahl: ", i + 1);
movl 8(%rsp), %ebx ; scanf ("%d", b + i); /* andere Schreibweise: &a[i] */
movslq %ebx, %rdi ; }
salq $2, %rdi ; a.data = b;
call malloc@PLT ; return a;
movq %rax, %r13 ; }
testl %ebx, %ebx ;
jle .L2 ; int main (void)
movq %rax, %rbp ; {
movl $0, %ebx ; dyn_array x = read_something ();
leaq .LC2(%rip), %r12 ; printf ("%d eingegebene Zahlen:", x.n);
.L3: ; for (int i = 0; i < x.n; i++)
addl $1, %ebx ; printf (" %d", x.data[i]);
movl %ebx, %esi ; printf ("\n");
movq %r12, %rdi ; free (x.data);
movl $0, %eax ; return 0;
call printf@PLT ; }
movq %rbp, %rsi
leaq .LC1(%rip), %rdi
movl $0, %eax
call __isoc99_scanf@PLT
addq $4, %rbp
cmpl 8(%rsp), %ebx
jl .L3
.L2:
movq %r13, (%rsp)
movq (%rsp), %rax
movq 8(%rsp), %rdx
addq $24, %rsp
.cfi_def_cfa_offset 40
popq %rbx
.cfi_def_cfa_offset 32
popq %rbp
.cfi_def_cfa_offset 24
popq %r12
.cfi_def_cfa_offset 16
popq %r13
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE22:
.size read_something, .-read_something
.section .rodata.str1.1
.LC3:
.string "%d eingegebene Zahlen:"
.LC4:
.string " %d"
.text
.globl main
.type main, @function
main:
.LFB23:
.cfi_startproc
pushq %r13
.cfi_def_cfa_offset 16
.cfi_offset 13, -16
pushq %r12
.cfi_def_cfa_offset 24
.cfi_offset 12, -24
pushq %rbp
.cfi_def_cfa_offset 32
.cfi_offset 6, -32
pushq %rbx
.cfi_def_cfa_offset 40
.cfi_offset 3, -40
subq $8, %rsp
.cfi_def_cfa_offset 48
call read_something
movq %rax, %r13
movq %rdx, %rbp
movl %edx, %esi
leaq .LC3(%rip), %rdi
movl $0, %eax
call printf@PLT
testl %ebp, %ebp
jle .L7
movq %r13, %rbx
leal -1(%rbp), %eax
leaq 4(%r13,%rax,4), %r12
leaq .LC4(%rip), %rbp
.L8:
movl (%rbx), %esi
movq %rbp, %rdi
movl $0, %eax
call printf@PLT
addq $4, %rbx
cmpq %r12, %rbx
jne .L8
.L7:
movl $10, %edi
call putchar@PLT
movq %r13, %rdi
call free@PLT
movl $0, %eax
addq $8, %rsp
.cfi_def_cfa_offset 40
popq %rbx
.cfi_def_cfa_offset 32
popq %rbp
.cfi_def_cfa_offset 24
popq %r12
.cfi_def_cfa_offset 16
popq %r13
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE23:
.size main, .-main
.ident "GCC: (Debian 8.3.0-6) 8.3.0"
.section .note.GNU-stack,"",@progbits
.file "array-param-12.c"
.text
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Wie viele Zahlen? "
.LC1:
.string "%d"
.LC2:
.string "%d. Zahl: "
.text
.globl read_something
.type read_something, @function
read_something:
.LFB22:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
pushl %edi
.cfi_def_cfa_offset 12
.cfi_offset 7, -12
pushl %esi
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
pushl %ebx
.cfi_def_cfa_offset 20
.cfi_offset 3, -20
subl $56, %esp
.cfi_def_cfa_offset 76
call __x86.get_pc_thunk.bx
addl $_GLOBAL_OFFSET_TABLE_, %ebx
leal .LC0@GOTOFF(%ebx), %eax
pushl %eax
.cfi_def_cfa_offset 80
call printf@PLT
addl $8, %esp ; #include <stdio.h>
.cfi_def_cfa_offset 72 ; #include <stdlib.h>
leal 36(%esp), %eax ;
pushl %eax ; typedef struct
.cfi_def_cfa_offset 76 ; {
leal .LC1@GOTOFF(%ebx), %eax ; int *data;
pushl %eax ; int n;
.cfi_def_cfa_offset 80 ; }
call __isoc99_scanf@PLT ; dyn_array;
movl 44(%esp), %esi ;
leal 0(,%esi,4), %eax ; dyn_array read_something (void)
movl %eax, (%esp) ; {
call malloc@PLT ; dyn_array a;
movl %eax, 28(%esp) ; printf ("Wie viele Zahlen? ");
addl $16, %esp ; scanf ("%d", &a.n);
.cfi_def_cfa_offset 64 ; int *b = malloc (a.n * sizeof (int));
testl %esi, %esi ; for (int i = 0; i < a.n; i++)
jle .L2 ; {
movl %eax, %edi ; printf ("%d. Zahl: ", i + 1);
movl $0, %esi ; scanf ("%d", b + i); /* andere Schreibweise: &a[i] */
leal .LC2@GOTOFF(%ebx), %ebp ; }
leal .LC1@GOTOFF(%ebx), %eax ; a.data = b;
movl %eax, 8(%esp) ; return a;
.L3: ; }
addl $1, %esi ;
subl $8, %esp ; int main (void)
.cfi_def_cfa_offset 72 ; {
pushl %esi ; dyn_array x = read_something ();
.cfi_def_cfa_offset 76 ; printf ("%d eingegebene Zahlen:", x.n);
pushl %ebp ; for (int i = 0; i < x.n; i++)
.cfi_def_cfa_offset 80 ; printf (" %d", x.data[i]);
call printf@PLT ; printf ("\n");
addl $8, %esp ; free (x.data);
.cfi_def_cfa_offset 72 ; return 0;
pushl %edi ; }
.cfi_def_cfa_offset 76
pushl 20(%esp)
.cfi_def_cfa_offset 80
call __isoc99_scanf@PLT
addl $4, %edi
addl $16, %esp
.cfi_def_cfa_offset 64
cmpl 28(%esp), %esi
jl .L3
.L2:
movl 12(%esp), %eax
movl %eax, 24(%esp)
movl 24(%esp), %eax
movl 28(%esp), %edx
movl 64(%esp), %ecx
movl %eax, (%ecx)
movl %edx, 4(%ecx)
movl %ecx, %eax
addl $44, %esp
.cfi_def_cfa_offset 20
popl %ebx
.cfi_restore 3
.cfi_def_cfa_offset 16
popl %esi
.cfi_restore 6
.cfi_def_cfa_offset 12
popl %edi
.cfi_restore 7
.cfi_def_cfa_offset 8
popl %ebp
.cfi_restore 5
.cfi_def_cfa_offset 4
ret $4
.cfi_endproc
.LFE22:
.size read_something, .-read_something
.section .rodata.str1.1
.LC3:
.string "%d eingegebene Zahlen:"
.LC4:
.string " %d"
.text
.globl main
.type main, @function
main:
.LFB23:
.cfi_startproc
leal 4(%esp), %ecx
.cfi_def_cfa 1, 0
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
.cfi_escape 0x10,0x5,0x2,0x75,0
movl %esp, %ebp
pushl %edi
pushl %esi
pushl %ebx
pushl %ecx
.cfi_escape 0xf,0x3,0x75,0x70,0x6
.cfi_escape 0x10,0x7,0x2,0x75,0x7c
.cfi_escape 0x10,0x6,0x2,0x75,0x78
.cfi_escape 0x10,0x3,0x2,0x75,0x74
subl $36, %esp
call __x86.get_pc_thunk.bx
addl $_GLOBAL_OFFSET_TABLE_, %ebx
leal -32(%ebp), %eax
pushl %eax
call read_something
movl -32(%ebp), %eax
movl %eax, %esi
movl %eax, -40(%ebp)
movl -28(%ebp), %edi
subl $12, %esp
pushl %edi
leal .LC3@GOTOFF(%ebx), %eax
pushl %eax
call printf@PLT
addl $32, %esp
testl %edi, %edi
jle .L7
leal (%esi,%edi,4), %eax
movl %eax, -36(%ebp)
leal .LC4@GOTOFF(%ebx), %edi
.L8:
subl $8, %esp
pushl (%esi)
pushl %edi
call printf@PLT
addl $4, %esi
addl $16, %esp
cmpl -36(%ebp), %esi
jne .L8
.L7:
subl $12, %esp
pushl $10
call putchar@PLT
addl $4, %esp
pushl -40(%ebp)
call free@PLT
addl $16, %esp
movl $0, %eax
leal -16(%ebp), %esp
popl %ecx
.cfi_restore 1
.cfi_def_cfa 1, 0
popl %ebx
.cfi_restore 3
popl %esi
.cfi_restore 6
popl %edi
.cfi_restore 7
popl %ebp
.cfi_restore 5
leal -4(%ecx), %esp
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE23:
.size main, .-main
.section .text.__x86.get_pc_thunk.bx,"axG",@progbits,__x86.get_pc_thunk.bx,comdat
.globl __x86.get_pc_thunk.bx
.hidden __x86.get_pc_thunk.bx
.type __x86.get_pc_thunk.bx, @function
__x86.get_pc_thunk.bx:
.LFB24:
.cfi_startproc
movl (%esp), %ebx
ret
.cfi_endproc
.LFE24:
.ident "GCC: (Debian 8.3.0-6) 8.3.0"
.section .note.GNU-stack,"",@progbits
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int *data;
int n;
}
dyn_array;
void read_something (dyn_array *a)
{
printf ("Wie viele Zahlen? ");
scanf ("%d", &a->n);
int *b = malloc (a->n * sizeof (int));
for (int i = 0; i < a->n; i++)
{
printf ("%d. Zahl: ", i + 1);
scanf ("%d", b + i); /* andere Schreibweise: &a[i] */
}
a->data = b;
}
int main (void)
{
dyn_array x;
read_something (&x);
printf ("%d eingegebene Zahlen:", x.n);
for (int i = 0; i < x.n; i++)
printf (" %d", x.data[i]);
printf ("\n");
free (x.data);
return 0;
}
.file "array-param-13.c"
.text
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Wie viele Zahlen? "
.LC1:
.string "%d"
.LC2:
.string "%d. Zahl: "
.text
.globl read_something
.type read_something, @function
read_something:
.LFB22:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
pushl %edi
.cfi_def_cfa_offset 12
.cfi_offset 7, -12
pushl %esi
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
pushl %ebx
.cfi_def_cfa_offset 20
.cfi_offset 3, -20
subl $40, %esp
.cfi_def_cfa_offset 60
call __x86.get_pc_thunk.bx ; #include <stdio.h>
addl $_GLOBAL_OFFSET_TABLE_, %ebx ; #include <stdlib.h>
movl 60(%esp), %edi ;
leal .LC0@GOTOFF(%ebx), %eax ; typedef struct
pushl %eax ; {
.cfi_def_cfa_offset 64 ; int *data;
call printf@PLT ; int n;
addl $8, %esp ; }
.cfi_def_cfa_offset 56 ; dyn_array;
leal 4(%edi), %eax ;
pushl %eax ; void read_something (dyn_array *a)
.cfi_def_cfa_offset 60 ; {
leal .LC1@GOTOFF(%ebx), %eax ; printf ("Wie viele Zahlen? ");
pushl %eax ; scanf ("%d", &a->n);
.cfi_def_cfa_offset 64 ; int *b = malloc (a->n * sizeof (int));
call __isoc99_scanf@PLT ; for (int i = 0; i < a->n; i++)
movl 4(%edi), %esi ; {
leal 0(,%esi,4), %eax ; printf ("%d. Zahl: ", i + 1);
movl %eax, (%esp) ; scanf ("%d", b + i); /* andere Schreibweise: &a[i] */
call malloc@PLT ; }
movl %eax, 28(%esp) ; a->data = b;
addl $16, %esp ; }
.cfi_def_cfa_offset 48 ;
testl %esi, %esi ; int main (void)
jle .L2 ; {
movl %eax, %ebp ; dyn_array x;
movl $0, %esi ; read_something (&x);
leal .LC2@GOTOFF(%ebx), %eax ; printf ("%d eingegebene Zahlen:", x.n);
movl %eax, 4(%esp) ; for (int i = 0; i < x.n; i++)
leal .LC1@GOTOFF(%ebx), %eax ; printf (" %d", x.data[i]);
movl %eax, 8(%esp) ; printf ("\n");
.L3: ; free (x.data);
addl $1, %esi ; return 0;
subl $8, %esp ; }
.cfi_def_cfa_offset 56
pushl %esi
.cfi_def_cfa_offset 60
pushl 16(%esp)
.cfi_def_cfa_offset 64
call printf@PLT
addl $8, %esp
.cfi_def_cfa_offset 56
pushl %ebp
.cfi_def_cfa_offset 60
pushl 20(%esp)
.cfi_def_cfa_offset 64
call __isoc99_scanf@PLT
addl $4, %ebp
addl $16, %esp
.cfi_def_cfa_offset 48
cmpl 4(%edi), %esi
jl .L3
.L2:
movl 12(%esp), %eax
movl %eax, (%edi)
addl $28, %esp
.cfi_def_cfa_offset 20
popl %ebx
.cfi_restore 3
.cfi_def_cfa_offset 16
popl %esi
.cfi_restore 6
.cfi_def_cfa_offset 12
popl %edi
.cfi_restore 7
.cfi_def_cfa_offset 8
popl %ebp
.cfi_restore 5
.cfi_def_cfa_offset 4
ret
.cfi_endproc
.LFE22:
.size read_something, .-read_something
.section .rodata.str1.1
.LC3:
.string "%d eingegebene Zahlen:"
.LC4:
.string " %d"
.text
.globl main
.type main, @function
main:
.LFB23:
.cfi_startproc
leal 4(%esp), %ecx
.cfi_def_cfa 1, 0
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
.cfi_escape 0x10,0x5,0x2,0x75,0
movl %esp, %ebp
pushl %edi
pushl %esi
pushl %ebx
pushl %ecx
.cfi_escape 0xf,0x3,0x75,0x70,0x6
.cfi_escape 0x10,0x7,0x2,0x75,0x7c
.cfi_escape 0x10,0x6,0x2,0x75,0x78
.cfi_escape 0x10,0x3,0x2,0x75,0x74
subl $36, %esp
call __x86.get_pc_thunk.bx
addl $_GLOBAL_OFFSET_TABLE_, %ebx
leal -32(%ebp), %eax
pushl %eax
call read_something
addl $8, %esp
pushl -28(%ebp)
leal .LC3@GOTOFF(%ebx), %eax
pushl %eax
call printf@PLT
addl $16, %esp
cmpl $0, -28(%ebp)
jle .L7
movl $0, %esi
leal .LC4@GOTOFF(%ebx), %edi
.L8:
subl $8, %esp
movl -32(%ebp), %eax
pushl (%eax,%esi,4)
pushl %edi
call printf@PLT
addl $1, %esi
addl $16, %esp
cmpl %esi, -28(%ebp)
jg .L8
.L7:
subl $12, %esp
pushl $10
call putchar@PLT
addl $4, %esp
pushl -32(%ebp)
call free@PLT
addl $16, %esp
movl $0, %eax
leal -16(%ebp), %esp
popl %ecx
.cfi_restore 1
.cfi_def_cfa 1, 0
popl %ebx
.cfi_restore 3
popl %esi
.cfi_restore 6
popl %edi
.cfi_restore 7
popl %ebp
.cfi_restore 5
leal -4(%ecx), %esp
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE23:
.size main, .-main
.section .text.__x86.get_pc_thunk.bx,"axG",@progbits,__x86.get_pc_thunk.bx,comdat
.globl __x86.get_pc_thunk.bx
.hidden __x86.get_pc_thunk.bx
.type __x86.get_pc_thunk.bx, @function
__x86.get_pc_thunk.bx:
.LFB24:
.cfi_startproc
movl (%esp), %ebx
ret
.cfi_endproc
.LFE24:
.ident "GCC: (Debian 8.3.0-6) 8.3.0"
.section .note.GNU-stack,"",@progbits
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment