There are some things you need to get right:
-
When you do printf
with %s
you are supposed to pass a pointer as a parameter, which was missing something:
printf("%s\n", link[i]);
Since you are passing a character, the most appropriate formatter would be %c
.
-
In scanf("%s", &link);
has &
more because scanf
has to receive the memory address where it will put the read data and link
because it is a pointer already refers to a memory address.
-
Failed to include <string.h>
which is where the strlen
function comes from
But if you just want to show the result you can simply printf
%s
by passing the base address that is link
and summing X
memory positions until you reach the last 11
, so there is no need for a loop :
linkSize = strlen(link);
printf("\nTamanho da String: %i\n", linkSize);
calcLink = linkSize - 11;
if (calcLink < 0) calcLink = 0; //nao deixar a posição ser negativa
//aqui soma ao ponteiro link as posições de memoria necessárias para chegar aos ultimos 11
printf("%s\n", link + calcLink);
Example of this code working on Ideone
If you want to construct a new string with this text, you simply have to allocate static or dynamic and copy the characters either manually or with memcpy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void last_link(char* link, int char_count, char* last_chars){
int linkSize = strlen(link);
int calcLink = linkSize - char_count;
if (calcLink < 0) calcLink = 0;
memcpy(last_chars, link + calcLink, char_count); //copiar os carateres
last_chars[char_count] = 'printf("%s\n", link[i]);
'; //colocar o terminador
}
int main() {
char link[100];
scanf("%s", link);
printf("\nTamanho da String: %i\n", strlen(link));
char last_chars[12]; //tem de ter mais 1 caractere para o terminador linkSize = strlen(link);
printf("\nTamanho da String: %i\n", linkSize);
calcLink = linkSize - 11;
if (calcLink < 0) calcLink = 0; //nao deixar a posição ser negativa
//aqui soma ao ponteiro link as posições de memoria necessárias para chegar aos ultimos 11
printf("%s\n", link + calcLink);
last_link(link, 11, last_chars); //obter os últimos 11
printf("%s\n", last_chars);
return 0;
}
Example of this code in Ideone
I could have done the creation of the new string
directly within the function last_link
with malloc
but this would imply to the caller the function of not being able to do free
otherwise would have a memory leak .