-7

i want to assign value of an integer array to integer pointer. The pointer takes a different value of assignment. Please help me on this. I have assigned 2 for port_val variable. But, after assigning that to ofport_request var, the value becomes different.

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>

int main ()
{
    printf("INSIDE MAIN..\n");
    int64_t of_port[100];
    int count=2;
    int i;
    int port_val = 2;
    int port_next_val = 4;
    size_t n_ofport_request = 1;
    int64_t *ofport_request = malloc(sizeof *ofport_request * (n_ofport_request));
    for(i=0;i<count;i++) {
            if(i == 0) {
                of_port[i] = port_val;
            } else {
                of_port[i] = port_next_val;
            }
            ofport_request[0] = of_port[i];
            printf("OFPORT VAL = %d\n",ofport_request);
    }
    return 0;
}
Barathi
  • 189
  • 1
  • 2
  • 11

1 Answers1

2

If you want to print the pointer variable, you have to use the * before the variable name.

Use the below printf statement it will work.

printf("OFPORT VAL = %lld\n",*ofport_request);
  • Yes, you wanted to say something? I did not get that coz u deleted. :) [see this](http://stackoverflow.com/a/37256166/2173917) for reference. – Sourav Ghosh May 17 '16 at 08:16
  • What is meant by UB ? –  May 17 '16 at 08:16
  • How you are passing the argument to that ovsrec_interface_set_ofport_request function. –  May 17 '16 at 10:15
  • @Barathi, You using the pointer variable. Make sure two points. 1. If you want to use that pointer variable value, you need the *. 2. If you want to use that as a pinter(address) you don't use the *. –  May 17 '16 at 10:19