0

I am trying to reverse a word using pointers but the problem that the following line cause error:access violation writting location
0x000...

*temp = *(n+len);

the full code

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string.h>
void re(char *n){
    int len = strlen(n);
    char *temp=NULL;
    while (len != 0){
        *temp = *(n+len);
        len--;
        temp++;
    }
    n = temp;
}


void main(){

    char name[] = "yahia";
    re(name);
    printf("%s \n", name);

    system("pause");
}

*microsoft compilar
any help ?

2
Contributors
2
Replies
68
Views
2 Years
Discussion Span
Last Post by janijani7
0

ok , i figured the solution ,if any one is interested!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void re(char *n){
    int len = strlen(n);
    char localArray[10];
    char *temp = localArray; /*the problem was i inialized it to null and this is not correct as i store in no location in mem*/
    int size = len--; /*to avoid the null terminal for proper memcpy operation*/
    while (len >= 0){
        *temp = *(n + len);
        len--;
        temp++;
    }
    memcpy(n, localArray, size);
}

char name[] = "yahia";
void main(){
    printf("%s \n", name);
    re(name);
    printf("%s \n", name);
    system("pause");
}
-1

Qualified tutors are ready to provide you Statistics Homework Help, Accounting Homework Help & assistance with all other subjects including College Finance, Economics Assignments. You can also contact us for your Dissertation, Thesis or your other writing needs. Best Prices, Fast Response!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.