-4

I'm new to C and I'm having a hard time understanding how to call methods with pointers. Currently this code should reverse a null-terminated string, but I get the errors

main.c:8:12: error: use of undeclared identifier 'sas'
         char* N = sas;
                   ^ main.c:10:10: warning: incompatible integer to pointer conversion passing 'char' to parameter of type
       'char *'; remove * [-Wint-conversion]
         reverse(*N);
                 ^~ ./header.h:3:27: note: passing argument to parameter 'N' here EXTERN void reverse(char *N);

My actual code is this: Main:

#include <stdio.h> 
#include <stdlib.h> 
#include "header.h"

int main(int argc, char *argv[]) 
{ 
    char* N = sas; 
    reverse(*N); 
}

reverse:

#include <stdio.h>
#include "header.h
#include <stdlib.h>

void reverse(char *str) 
{
    char* end = str; 
    char temp; 
    printf("this is *str: %c\n", *str); 
    if (str) 
    { 
        while (*end) 
        {
            ++end: 
        } 
        end--; 
        while (str < end) 
        { 
            temp = *str
            *str++ = *end; 
            *end-- = temp; 
        }
    }
 }

header.h:

#define EXTERN extern
EXTERN void reverse(char *N) 

thanks for the help and time!

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
chris123
  • 197
  • 3
  • 13
  • 1
    1. Please choose a better coding style, this style is very difficult to follow, 2. What is `sas`? what are you trying to do? 3. Your code has several reasons not to compile, for example you have `++end:`??? and a lot of closing `}` braces that apparently are not needed, though I suspect you might have left code out. – Iharob Al Asimi Jun 05 '15 at 21:09
  • 2
    You probably need `char *N = "sas";` (a string), and you should call `reverse(N)`. Then you'll just crash because strings aren't modifiable. So, in reality, you need `char N[] = "sas";`. Of course, it will be hard to spot the difference between the original and the reversed string. You probably should test with `char N[] = "xyz";` or something else asymmetric. You're printing in `reverse()` is going to print a single character, not the entire string. You should print the 'after' string in `main()`, too. Otherwise, you won't know whether your code made the correct change. – Jonathan Leffler Jun 05 '15 at 21:13
  • `reverse()` has no block. Please post complete code, stripped down to the relevant parts. – too honest for this site Jun 05 '15 at 21:16
  • sorry, I'm working off an interview book and this is the code they have -- besides the main method -- so the reverse method is the whole method. I'm just confused on how to actually call the thing. I apologize for the poorly written code, but I'm am a foreigner to C. – chris123 Jun 05 '15 at 21:28
  • possible duplicate of [How do function pointers in C work?](http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work) – delirium Jun 05 '15 at 21:34

3 Answers3

1

First of all, there're many syntax errors within that piece of code:

  1. 'sas' what is that? your compiler thinks it's a variable but can't find any with that name. if you wanted to put a "sas" string, then:

    char* N = "sas";
    
  2. inconsistant brackets. More closing brackets than opening ones, and no opening bracket after declaring your function.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Dleep
  • 1,045
  • 5
  • 12
1
int main(int argc, char *argv[]) 
{ 
    char* N = "sas"; 
    reverse(*N); 
}

First you make N point to a string constant. Then you try to reverse what N points to. But since N points to a string constant, you're trying to reverse a string constant. By definition, constants cannot have their values changed.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
-3

As I understand it, you are trying to reverse a string. This is just a slight modification of your code.

Full Source:

#include <stdio.h> 
#include <stdlib.h> 

void reverse(char *N);

int main(int argc, char *argv[]) 
{ 
  char strSas[] = "sas";
  reverse(strSas);
}

void reverse(char *str)
{
  char* end = str; 
  char temp; 
  if(str) {       
    printf("this is *str: %c\n", *str); 
    while(*end) {
      ++end;
    } 
    end--; 
    while(str < end) { 
      temp = *str;
      *str = *end; 
      *end = temp;

      ++str;
      --end;
    }
  }
}
Nick Banks
  • 4,298
  • 5
  • 39
  • 65