There are some things you need to get right:
When does printf with %s is supposed to pass a pointer as parameter, which was something that was missing in:
printf("%s\n", link[i]);
Since you are passing a character the most suitable formatter would be %c.
In scanf("%s", &link); has a & the more the scanf must receive the memory address where it will put the read and link for being a pointer already refers to a memory address.
Failed to include <string.h> which is where the function comes from strlen
But if you just want to show the result you can simply do printf %s passing the base address that is link and adding up X Memory positions until reaching the last 11, 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 build a new string with this text simply allocate statically or dynamically and copy the characters both manually and 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] = '\0'; //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 \0
last_link(link, 11, last_chars); //obter os últimos 11
printf("%s\n", last_chars);
return 0;
}
Example of this code also in Ideone
I could have created the new string directly within the function last_link with malloc but that would imply that whoever called it the function of not being able to forget to do free otherwise would have a memory leak.
Interesting. Every time I explore this forum I am more surprised by how much knowledge we can acquire here. It’s nice to see experienced people are willing to help neophytes like me. Thank you for your answer, it was very helpful, I never really understood the operation of pointers, in fact I never really cared much. I see that this directly affects the development of a program. I thank you for your help!
– Henrique Marques
@I’m glad I could help. When programming in C, unless they are very simple things it is common to have to deal with pointers, and so it is important to know how to work with them.
– Isac