Segmentation fault (core dumped) when accessing String

0

I've assembled the code below to separate only the last few characters of a link (the last 11 to be more exact). Until then everything works well, the calculations are done normally, the values also beat. The problem is at the time of giving printf in the values of the String (vector), the problem is returned Segmentation fault (core dumped) .

#include <stdio.h>
#include <string.h>

int main(){

  char link[100];
  int linkSize, calcLink;

  scanf("%s", &link);
  //lê o tamanho da string pra capturar os últimos caracteres
  linkSize = strlen(link);

  printf("\nTamanho da String: %i\n", linkSize);

  calcLink = linkSize - 11;

  printf("%s\n", link);

  int i = calcLink;
  while(i < linkSize){
    printf("%s\n", link[i]);
    ++i;
  }
  return 0;
}

How to access the last memory addresses of my String link and how to save the last characters in another string. I hope I have been clear. I know it's a very basic thing but no matter how hard I tried I could not find an answer that could solve this particular case. Thanks in advance for your understanding.

    
asked by anonymous 09.01.2018 / 01:51

1 answer

3

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 .

    
09.01.2018 / 02:46